Aleksey Vladimirovich wrote:Oh, small remark: the reason to call Thread2 and make Thread1 to wait is that in real code there will be N (N > 1) child threads, each doing it's own job. And once all child threads are done, parent thread sould wake up and do it's job.
Joanne
Jesper de Jong wrote:If the thread that triggers the operation in a second thread has to wait for the result, then what's the point of doing the work in a separate thread? You could just as well do it in a single thread then.
Joanne Neal wrote:
Aleksey Vladimirovich wrote:Oh, small remark: the reason to call Thread2 and make Thread1 to wait is that in real code there will be N (N > 1) child threads, each doing it's own job. And once all child threads are done, parent thread sould wake up and do it's job.
So if the parent thread doesn't need to do anything until all the child threads have completed why don't you just get the parent thread to call join on each of the children after it has started them ?
Jesper de Jong wrote:Your "small remark" changes the whole question...
If you have multiple threads doing the work, you could use a CountDownLatch. Create it with a starting number, the number of tasks that has to be performed. In the computation thread, count down the latch by one when the task is finished. In the main thread, make it wait until the CountDownLatch reaches zero (it has special methods for that). When it reaches zero, all tasks are done and you can collect the result.
Aleksey Vladimirovich wrote:Sorry, I'm noob in Java. So, if I call join on all child threads, I'll be able to get data from them? I mean the parent thread won't wake up until all of them are finished? And here is what java docs say: "join() - Waits for this thread to die." So will I be able to get my data from them?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jayesh A Lalwani wrote:Since executor service is not available to you, you are better off implementing your own executor service. You can use a publisher consumer pattern. Have a queue that holds your jobs. The main thread posts jobs to the queue, and the worker threads takes jobs from the queue and process them. Have another queue that holds results, and the worker threads can posts results to the result queue as they finish a job. The main thread can monitor size of the queues. Once the results queue have all the results, take them out and process them.
Jesper de Jong wrote:You start a number of threads, so you have a collection of Thread objects. Then you just loop over those Thread objects and call join() on each of them in the loop.
If the thread you call it on has already stopped, join() returns immediately. If it's still running, join() waits until the thread has stopped.
So if you call it in a loop on all Thread objects, then when the loop is done, all those threads have stopped.
Aleksey Vladimirovich wrote:You're saying that the parent thread waits for a response from each thread, join() method was called on, and only then does the same to the next thread (second child won't start until first is done), right? In that case there is no point in it
Joanne
Joanne Neal wrote:
Aleksey Vladimirovich wrote:You're saying that the parent thread waits for a response from each thread, join() method was called on, and only then does the same to the next thread (second child won't start until first is done), right? In that case there is no point in it
You store a reference to each thread in a collection. You then loop through the collection and call start on each thread. So all your threads are now running. You then loop through the collection again and call join on each thread. The call to join on the first thread won't return until the thread has completed, but during this time all the other threads are running as well. Once the call to join on the first thread returns, you call join on the second thread. It may be that the second thread has already completed, so join will return immediately but if it hasn't join will not return until it has. This repeats for all the threads in your loop.
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|