Reputation: 5308
I have started four threads (multithreading) & assigned a task for each of the threads. The task for each of the thread is uploading a file to the server
I want to perform the following functionalities.
i>I want to assign the next set of files to the 4 new threads only when all the existing 4 threads has completed its execution.
How can I determine whether all the existing 4 Threads has completed their execution ?
I used the condition
for (Thread thread : threads) {
if(thread.getState() == Thread.State.TERMINATED || getAlive()) == false){
++ counter ;
}
}
to find the status of the threads .
If the counter value becomes 4 , then it means that all the threads are not alive or terminated.
I get a notification for every file which is getting uploaded.
After I found that the counter value = 4 , I again assigned four new files to be uploaded using four new threads.
There can be a situation where a file didn't get uploaded the first time , so I need to reassign it again . I found out that , I am reassigning a file which is already uploaded. So , its a duplicate upload. I assigned the file again since I received no notification for the upload successfull for that specific file.
Also , it can be that the code mentioned above is not giving the proper status of the threads. It can be that the thread was still performing the operation when I reassigned the file again to be uploaded.
Since I used four threads , counter = 4 means that all the threads have performed their operation.
What should be my approach in solving the issue ?
Upvotes: 0
Views: 109
Reputation: 1
Here is the good one.. waiting for threads to complete
http://javahowto.blogspot.com/2007/05/when-to-join-threads.html
Upvotes: 0
Reputation: 415
is it mandatory to upload files in set of 4
Future can be used to block til operation is complete http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html
Upvotes: 0