Skip to content
Advertisement

Tag: concurrency

How to iterate on concurrentLinkedQueue by multiple threads?

In my application data producing speed (which is stored in in concurrentLinkedQueue) is greater than i can consume with single thread. I have decided to start with creating 4 threads to consume the data, to prevent my application from “out of memory exception”. Questions : Any other better design for the above problem with an example ? Can we iterate

How to run 2 methods concurrently in same class with Java

I would like to use 2 methods in the same class concurrently using the same object in Java. For example: And using this methods in another method: note: They don’t have to be synchronized. Answer There are several ways to achieve your task. You have quiet easy situation when threads should not be synchronized. You can use ExecutorService from Java

How to combine 3 or more CompletionStages?

If have 2 CompletionStages, I can combine them with thenCombine method: If I have 3 or more CompletionStages, I can make a chain of thenCombine methods, but I have to use temporary objects to pass results. For example, here is a solution using Pair and Triple from the org.apache.commons.lang3.tuple package: Is there a better way to combine results from multiple

CompletableFuture from Callable?

Today I experimented with the “new” CompletableFuture from Java 8 and found myself confused when I didn’t find a runAsync(Callable) method. I can do it myself like shown below, but why is this (to me very obvious and useful utility method) missing? Am I missing something? Answer You are supposed to use supplyAsync(Supplier<U>) In general, lambdas and checked exceptions do

What state is a sleeping thread in?

I’m looking for verification/arguments on the following: A thread is in exactly one of the 5 (+1 — 2 for WAITING) states at any point of time. Suppose a thread T calls Thread.sleep(3000); and thus puts itself into sleep for 3 secs. Which state is it in during those 3 secs? It’s clearly start()-ed and is still alive, thus is

Thread is interrupted by calling interrupt(), but Thread.isInterrupted() returns false

I am testing InterruptedException with the following test code: In run() , I interrupt() current working thread, and caught a InterruptedException. In main thread, my last line of code is a System.out.println(…) which prints out the interrupt status of working thread. Since I have caught InterruptedException in run(), I though I should get the message that workingThread.isInterrupted() is true, but

Actual use of lockInterruptibly for a ReentrantLock

What do you actually use for this method lockInterruptibly? I have read the API however it’s not very clear to me. Could anybody express it in other words? Answer The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt signal sent to it from another thread. How this particular

Random errors when changing series using JFreeChart

I’m making a GUI that display result of background calculations. But before that, I wanted to test changing the dataset. Here is my code: As you can see, I want to change points on the graph (every time it finishes ‘some complicated computations’) – this change is in the thread invoked by me in another class. My problem is that

Advertisement