I use Thread in Java. I create a class that extend Thread and it’s fine, but the problem is, when call this Thread, I don’t know the number of instances of this class, since the user must enter this number. For example:
multiThread multiThreadInstance = new multiThread(/* number entered from user */);
multiThreadInstance.start();
This will call this thread once, but if i write:
multiThread multiThreadInstance1 = new multiThread(/* number entered from user */)
multiThreadInstance1.start()
multiThread multiThreadInstance2 = new multiThread(/* number entered from user */)
multiThreadInstance2.start()
This will call it twice at the same time, and so on.
If I put it in a for loop, then if user enters 3 for example, then start1 runs, when start1 finishes, start2 runs, when start2 finishes, start3 runs. I need to keep these instances of thread running in at the same time. How can i do this?
You need to use java high level concurrency utilities to do this. Look at countdownlatches and executors. The following is a code that would do what you want. I recommend you read up on java concurrency utilities.
A runnable version of the code example provided above: (Edited)