I have few threads that performs an actvity in parallel. I would have to proceed with the subsequent actvity only after all the threads are completed. Is there a way to achieve this?
You should take a look at the join() method in the Thread class. A join can be used to allow one thread to wait for another to complete. It's also overloaded to allow for the specification of a time to wait for the other thread to finish. Joins are discussed as part of the Java Tutorial on Concurrency.
for all threads do thread.join().
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()
You can use Thread.join() in order to cause a wait until thread completion. Take a look at the Javadoc for more information.
I think using a http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html is actually preferable over Thread.join() as it should scale better.
Conceptually, a way to address your problem is to create an observer object which will be referenced by every threads you are running. Once a thread is done it notifies the observer that it performed his task. Each time the Observer receive this kind of notification, it increment a counter, once the counter reach the number of threads, this means that all the threads are completed. then the Observer can start the final task.
That is for the theory. If you want there is a built-in java class for that: CountDownLatch
Related
Let's say that i have 10 active threads and only 3 resources (of something)
while the first three threads got the resources i want all other thread that try to get the resource to wait but that the wake up or notify will be in f.i.f.o order i mean that the first thread that got the waiting will be the first to wake up.
thank you all.
I think this link explains it quite well: https://www.safaribooksonline.com/library/view/java-threads-second/1565924185/ch04s03.html
When using notify it is impossible to decide or determine in advance which thread will be allowed to execute. I see 2 solutions to this:
Use notifyAll() and let each thread check for itself whether whose turn it is (e.g. by using a synchronised FIFO queue)
Use the method described in the link: let each thread wait on a different object and use 1 thread that has as it's sole purpose to notify the correct object. This seems like the best solution to me.
Java generally doesn't decide these things however if you use a fair lock e.g.
Lock lock = new ReentrantLock(true);
then those threads will acquire the lock in the order they were attempted. This works by disregarding the order thread would be notified and ensuring a lock which is not taken unless the thread is next on the FIFO queue.
and excuse the lack of knowledge on multithreaded apps, but I am new to the field.
Is there a pattern or common used methodology for monitoring the 'job completion' or 'job status' of worker threads from a monitor (a class that acts as a monitor)?
What I have currently done is create a list of workers and create one thread for each worker. After all threads have started i am looping over the worker list and 'checking their status' by making a call to a method.
At that time I couldn't come up with a different solution, but being new to the field, I don't know if this is the way to go, or if there are other solutions or patterns that I should study.
Depending on what you want, there are many ways that you can do this.
If you just want to wait until all the threads finish (i.e. all you care about is having everything finish before moving on), you can use Thread.join():
try {
for (Thread t: threadsIWaitOn)
t.join();
} catch (InterruptedException iex) {
/* ... handle error ...
}
If you want a more fine-grained control over the thread status and want to be able, at any time, to know what threads are doing, you can use the Thread.getState() function. This returns a Thread.State object that describes whether the thread is running, blocked, new, etc., and the Javadoc specifically says that it's designed for monitoring the state of a thread rather than trying to synchronize on it. This might be want you want to do.
If you want even more information than that - say, how to get a progress indicator for each thread that counts up from 0 to 100 as the thread progresses - then another option might be to create a Map from Threads to AtomicIntegers associating each thread with a counter, then pass the AtomicInteger into the constructor of each thread. That way, each thread can continuously increment the counters, and you can have another thread that continuously polls the progress.
In short, you have a lot of options based on what it is that you're trying to accomplish. Hopefully something in here helps out!
Use a ThreadPool and Executor, then you get a Future<> and you can poll for their completion and some more nice stuff, too. I can appreciate this book for you: Java Concurrency in Practice
Try to use any kind of synchronization. For example, wait on some kind of monitor/semaphore until job is done / whatever you need.
This is one question, asked to me a interview, on which I have no idea what he is asking.
If you can help on the same:
sleep, wait, notify, yield - which one is a callback?
None of the methods you list are callbacks. The entire Thread class contains only one user-overridable method, and that is run, which may be considered a callback method for that class because it is called by Thread's internals. However, a best practice is not to extend Thread at all. Supply your own Runnable implementation, which has its callback run method.
None of those look like traditional callbacks. A callback function/method is something you register to be called once an operation is complete (possibly asynchronously if the task is scheduled in another thread).
Sleep, wait and yield essentially block execution until their conditions are met. Notify wakes threads blocked by wait.
A callback is a method that is created to be called at a certain time/event from elsewhere.
sleep(), wait(), and yield() are called by the thread to perform an action. notify() may be interpreted as one, and as such is the more correct answer if one is correct, though none are.
I'm working on a project where execution time is critical. In one of the algorithms I have, I need to save some data into a database.
What I did is call a method that does that. It fires a new thread every time it's called. I faced a runoutofmemory problem since the loaded threads are more than 20,000 ...
My question now is, I want to start only one thread, when the method is called, it adds the job into a queue and notifies the thread, it sleeps when no jobs are available and so on. Any design patterns available or examples available online ?
Run, do not walk to your friendly Javadocs and look up ExecutorService, especially Executors.newSingleThreadExecutor().
ExecutorService myXS = Executors.newSingleThreadExecutor();
// then, as needed...
myXS.submit(myRunnable);
And it will handle the rest.
Yes, you want a worker thread or thread pool pattern.
http://en.wikipedia.org/wiki/Thread_pool_pattern
See http://www.ibm.com/developerworks/library/j-jtp0730/index.html for Java examples
I believe the pattern you're looking for is called producer-consumer. In Java, you can use the blocking methods on a BlockingQueue to pass tasks from the producers (that create the jobs) to the consumer (the single worker thread). This will make the worker thread automatically sleep when no jobs are available in the queue, and wake up when one is added. The concurrent collections should also handle using multiple worker threads.
Are you looking for java.util.concurrent.Executor?
That said, if you have 20000 concurrent inserts into the database, using a thread pool will probably not save you: If the database can't keep up, the queue will get longer and longer, until you run out of memory again. Also, note that an executors queue is volatile, i.e. if the server crashes, the data in it will be gone.
I'm implementing a parallel, performance-critical algorithm with multiple threads. I assign all threads some data to work on. When all those threads have finished to work on their data, I assign all threads new data, and the cycle continues. (This is what I refer to as thread "clocking" since it's somewhat similar to CPU clocking.)
What I came up with so far is using a master thread that stores an integer. At the beginning of each cycle, I set the integer to the number of slave threads. When a slave thread is done, it decrements the master thread's integer. Once that integer reaches zero, I start a new cycle.
Is this a good approach, or are there more efficient ways of doing the same thing?
You'd be better off using a Phaser (if you have Java 7), or CyclicBarrier for Java 5+.
I would recommend looking at the newer classes in the java.util.concurrent package, especially ThreadPoolTaskExecutor. You might be reinventing the wheel if you haven't looked beyond java.lang.Thread.
Well. See CyclicBarrier (JavaDoc)
A better way is to use Thread.join(). In you main thread, you call join() on all the threads you are starting. The main thread will wait untill all joined threads are finished.
See for example http://javahowto.blogspot.com/2007/05/when-to-join-threads.html
An ExecutorService can do this for you.
ExecutorService executor = Executors.newFixedThreadPool(10);
do {
List<Callable> tasks = getNextTasksToExecute();
executor.invokeAll(tasks);
} while (tasks.size() > 0);
This will create a thread pool with 10 threads. It will then call getNextTasksToExecute() which you should implement yourself to return the next bunch of tasks that need doing. It will execute those tasks in parallel in the thread pool and then keep looping until getNextTasksToExecute() returns no more tasks.
Edit:
Code not tested, think there may be a compile error, but you can figure that out.