Thread ordering
Hi all
I have a little program that uses a barrier that makes a number of threads wait until a threshold has been reached.
When this happens those threads are released and thereafter all threads pass.
See below.
publicclass BarrierDemo{
staticint noOfThreads = 20;
staticint barrierSize = 10;
staticint interval = 500;
//static Threads[] threads;
static Barrier barrier;
publicstaticvoid main(String args[]){
System.out.println("Number of threads = "+noOfThreads);
System.out.println("Barrier Size = "+barrierSize);
System.out.println("Interval = "+interval);
barrier =new Barrier(barrierSize);
for (int i=0 ; i<noOfThreads ; i++){
new Threads(barrier, i).start();
try{
Thread.sleep(interval);
}
catch (java.lang.InterruptedException e){
System.out.println("sleep interupted (yawn :o).");
System.out.println(" Exception - "+e+".");
}
}
}
privatestaticclass Threadsextends Thread{
Barrier barrier;
int threadNo;
Threads(Barrier b,int i){
barrier = b;
threadNo = i;
}
publicvoid run(){
try{
System.out.println("Thread "+ threadNo +" is waiting at the barrier");
barrier.barrierControl();
System.out.println("Thread "+ threadNo +" passed the barrier");
}
catch (java.lang.InterruptedException e){
System.out.println(e);
}
}
}
privatestaticclass Barrier{
privateint count = 0;
privateint barrierSize;
Barrier(int i){
barrierSize = i;
}
synchronizedvoid barrierControl()throws InterruptedException{
count++;
while (count ><= barrierSize){
wait();
}
notifyAll();
}
}
}
The output should be like this..
Number of threads = 20
Barrier Size = 10
Interval = 500
Thread 0 is waiting at the barrier
Thread 1 is waiting at the barrier
Thread 2 is waiting at the barrier
Thread 3 is waiting at the barrier
Thread 4 is waiting at the barrier
Thread 5 is waiting at the barrier
Thread 6 is waiting at the barrier
Thread 7 is waiting at the barrier
Thread 8 is waiting at the barrier
Thread 9 is waiting at the barrier
Thread 10 is waiting at the barrier
Thread 0 passed the barrier
Thread 1 passed the barrier
Thread 2 passed the barrier
Thread 3 passed the barrier
Thread 4 passed the barrier
Thread 5 passed the barrier
Thread 6 passed the barrier
Thread 7 passed the barrier
Thread 8 passed the barrier
Thread 9 passed the barrier
Thread 10 passed the barrier
Thread 11 is waiting at the barrier
Thread 11 passed the barrier... etc.
It usually does this. However, the ordering is not guaranteed. I thought about using an array so that I could find each specific thread in turn and notify() something like this...
publicclass BarrierDemo{
staticint noOfThreads = 20;
staticint barrierSize = 10;
staticint interval = 500;
static Threads[] threads;
static Barrier barrier;
publicstaticvoid main(String args[]){
System.out.println("Number of threads = "+noOfThreads);
System.out.println("Barrier Size = "+barrierSize);
System.out.println("Interval = "+interval);
threads =new Threads[noOfThreads];
barrier =new Barrier(barrierSize);
for (int i=0 ; i<noOfThreads ; i++){
threads[i] =new Threads(barrier, i);
threads[i].start();
//new Threads(barrier, i).start();
try{
Thread.sleep(interval);
}
catch (java.lang.InterruptedException e){
System.out.println("sleep interupted (yawn :o).");
System.out.println(" Exception - "+e+".");
}
}
}
//private static void notifications(int t) {
//threads[t].notify();
//}
privatestaticclass Threadsextends Thread{
Barrier barrier;
int threadNo;
Threads(Barrier b,int i){
barrier = b;
threadNo = i;
}
publicvoid run(){
try{
System.out.println("Thread "+ threadNo +" is waiting at the barrier");
barrier.barrierControl(threadNo);
System.out.println("Thread "+ threadNo +" passed the barrier");
}
catch (java.lang.InterruptedException e){
System.out.println(e);
}
}
}
privatestaticclass Barrier{
privateint count = 0;
privateint barrierSize;
Barrier(int i){
barrierSize = i;
}
synchronizedvoid barrierControl(int threadID)throws InterruptedException{//passports please :o)
count++;
while (count ><= barrierSize){
wait();
}
for (int i=0 ; i<noOfThreads ; i++){
threads[i].notify();
//BarrierDemo.notifications(i);
}
//notifyAll();
}
}
}
but that doesn't work.
The output is then like this;
...
... Thread 9 is waiting at the barrier
Thread 10 is waiting at the barrier
Exception in thread "Thread-10" java.lang.IllegalMonitorStateException: current
thread not owner... etc.
I also thought about changing their priorities but from what I have read this does not guarantee the order either.
Can anyone advise me how I can strictly enforce the order ?
Cheers>

