Java, Software Development

Wait Until All Threads Complete Execution in Java

I must say that these codes are based on the client codes I used to work on. I do not recommend using these codes. There are solutions already available in the Java SDK.

Main Class

The codes need to know the number of threads to process. The ThreadWaitBean keeps this number to compare with the number of threads actually completed. The basic idea is that each thread has access to a single ThreadWaitBean object. Right before a thread completes, it runs the executionComplete method. That method adds 1 to the number of threads actual completed.

The Wait Bean

This class keeps count of expected and actually completed threads count.

The Thread class

This class represents a task. It sleeps for 10 seconds to simulate heavy processing and runs the executionComplete method.

Demo and Output

If we run the codes, we would get the following in our console.

Sample output

Sample output

You Might Also Like