import java.lang.*;
class MyRun implements Runnable
{
public static int start = 0;
public void run()
{
int myCounter = 0;
System.out.println(Thread.currentThread().getName()+" is waiting on start flag");
while(start<1)
{
// just waiting...
}
while(myCounter<5)
{
myCounter++;
System.out.println(Thread.currentThread().getName()+" : "+myCounter);
try
{
Thread.sleep(1000);
} catch(Exception ex) {}
}
System.out.println(Thread.currentThread().getName()+" stopped and start="+start);
}
}
public class test
{
public static void main(String[] args)
{
Thread[] threads = new Thread[10];
for(int i=0; i<10; i++)
{
int p = i+1;
threads[i] = new Thread(new MyRun());
threads[i].setName("TH"+p);
threads[i].setPriority(p);
threads[i].start();
}
MyRun.start=1; // signaling GO
}
}
Output:
TH1 is waiting on start flag
TH2 is waiting on start flag
TH3 is waiting on start flag
TH4 is waiting on start flag
TH5 is waiting on start flag
TH6 is waiting on start flag
TH8 is waiting on start flag
TH7 is waiting on start flag
TH9 is waiting on start flag
TH10 is waiting on start flag
TH10 : 1
TH10 : 2
TH10 : 3
TH10 : 4
TH10 : 5
TH10 stopped and start=1
I'm expecting output from all threads as start flag is set to 1 but only thread with name TH10 is executed.
Can you help me to find out whats happening in this code with threads exactly ?
What i'm trying to do with such code ?
Ans : Trying to analyze priority of threads (clearing my thread concepts specially multi-threading with shared static variable over runnable).
Thanks in advance, let me know if you need any info from my side.
Update : consider all threads as horses, and when MyRun.start is set to 1 (it should be considered as GO signal for all horses) but only the 10th horse is running after signal.
This code has at least two problems:
start
) which is always a code smell as far as multi-threading is concerned => there is no guarantee that your threads see the correct value for start
The proper way to achieve this kind of things is by using paradigms in the java.util.concurrent
package. In this case this would be CountDownLatch
es
The example provided in the Javadoc is actually very close to what you are trying to achieve here (start all threads together, wait for them to complete), so I am reproducing it. MyRun
is the Worker
below; in doWork()
, please implement the content of your run()
method
The startSignal
"latches" the start of all the threads which are all "awaiting" it.
The doneSignal
"latches" the end of the program (the main
) once the end of all threads has been "counted down"
class Driver { // ...
void main() throws InterruptedException {
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);
for (int i = 0; i < N; ++i) // create and start threads
new Thread(new Worker(startSignal, doneSignal)).start();
doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
}
}
class Worker implements Runnable {
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;
Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
this.startSignal = startSignal;
this.doneSignal = doneSignal;
}
public void run() {
try {
startSignal.await();
doWork();
doneSignal.countDown();
} catch (InterruptedException ex) {} // return;
}
void doWork() { ... }
}