java thread problem

Associate
Joined
23 Aug 2004
Posts
1,493
I have a barrier class in java. The idea is threads are held at a barrier until the release method is called at which point they can pass the barrier. When I try and call the release method, nothing happens. I've tried using system.out.println to test whether the method is ever reached and it doesn't output. I believe that the thread is stuck in the checkIN method. Here's my code, can anyone see the flaw that would prevent me using the release method?

Many thanks!

Code:
public class Worker extends Thread {
 
   public static final int N = 50; //say
   private static double[] a = new double[N]; 
   private static double[] b = new double[N];
   private static double[] c = new double[N]; 
   private static SignallingBarrier b1 = new SignallingBarrier(N);

   private int index;

   public Worker(int index) {
      this.index = index;
   }
   
   
   public synchronized void run() {
     b1.checkIn();
     b1.release();
   }
   // other methods of worker
   // e.g. static accessor methods for the arrays a, b and c
}

Code:
public class SignallingBarrier {
	
	private int threadWaiting;
	private boolean releasing;
	private int numThreads;
	
	public SignallingBarrier(int N) {
		numThreads = N;
		threadWaiting = 0;
		releasing = false;
	}
	
	public synchronized void checkIn() {
		//prevent extra threads arriving during the release
		// from sneaking through
		
		while(releasing) {
			try {
				wait();
			} catch(InterruptedException e ) {};
			
		} 
		System.out.println("There are : " + threadWaiting + " threads waiting");
		threadWaiting++;
		
		//wait for release to arrive.
		while (!releasing) {
			try {
				wait();
			} catch(InterruptedException e) {};
			
		}
		
		threadWaiting--;
		
	}
	
	public synchronized void release() {
		System.out.println("There are : " + threadWaiting + " threads waiting abc");
		if (threadWaiting > 0){
			releasing = true;
			notifyAll();
			while (threadWaiting > 0){
				try {
					wait();
				}catch (InterruptedException e) {};
			}	
			
			if(threadWaiting == 0){
				releasing = false;
				notifyAll();
			}
		}
	}
}

Code:
public class TestBarrier{
	
	public static void main(String args[])
	throws InterruptedException {
		
		final int N = Worker.N;
		
		Worker[] a = new Worker[N];
		
		// create threads and add to the array
		for(int i = 0; i < N; i++)
		{
			a[i]= new Worker(i);
		}
		
		// start the N threads
		for(int i = 0; i < N; i++)
		{
			a[i].start();
		}
		
		//wait for all the slaves to terminate
		for(int i = 0; i < N; i++)
		{
			a[i].join();
		}	
	}
}
 
Going to Cali said:
I have a barrier class in java. The idea is threads are held at a barrier until the release method is called at which point they can pass the barrier.

What are the exact semantics here? I take it that as many threads as u want can stop at the barrier and that they are all released on a single call of release?

Going to Cali said:
Code:
public class Worker extends Thread {
 
   public static final int N = 50; //say
   private static double[] a = new double[N]; 
   private static double[] b = new double[N];
   private static double[] c = new double[N]; 
   private static SignallingBarrier b1 = new SignallingBarrier(N);

   private int index;

   public Worker(int index) {
      this.index = index;
   }
   
   
   public synchronized void run() {
     b1.checkIn();
     b1.release();
   }
   // other methods of worker
   // e.g. static accessor methods for the arrays a, b and c
}

Its clear why the release method wont ever be invoked. . .because you are using the same threads to checkIn and release? Obviously every thread will block in checkIn at one of the wait() lines and so release() will never be called by any of them!

Going to Cali said:
Code:
public class SignallingBarrier {
	
	private int threadWaiting;
	private boolean releasing;
	private int numThreads;
	
	public SignallingBarrier(int N) {
		numThreads = N;
		threadWaiting = 0;
		releasing = false;
	}
	
	public synchronized void checkIn() {
		//prevent extra threads arriving during the release
		// from sneaking through
		
		while(releasing) {
			try {
				wait();
			} catch(InterruptedException e ) {};
			
		} 
		System.out.println("There are : " + threadWaiting + " threads waiting");
		threadWaiting++;
		
		//wait for release to arrive.
		while (!releasing) {
			try {
				wait();
			} catch(InterruptedException e) {};
			
		}
		
		threadWaiting--;
		
	}
	
	public synchronized void release() {
		System.out.println("There are : " + threadWaiting + " threads waiting abc");
		if (threadWaiting > 0){
			releasing = true;
			notifyAll();
			while (threadWaiting > 0){
				try {
					wait();
				}catch (InterruptedException e) {};
			}	
			
			if(threadWaiting == 0){
				releasing = false;
				notifyAll();
			}
		}
	}
}

This code is very very strange. You have got a lot more waiting going on than you need and I'm (pretty sure) it wont work. If you clarify the exact semantics of what the barrier is supposed to do I could try and come up with something.
 
Last edited:
Thanks for your input. To try and clarify, any number of threads can arrive at the barrier and be stopped (using the checkIn method). A call to the release method should allow the threads to pass the barrier.
 
Why can't you just implement a barrier like this then (I haven't tested this yet):

Code:
public class SignallingBarrier {
	
	public SignallingBarrier() {

	}
	
	public synchronized void checkIn() {
		
		try {
			wait();
		} catch(InterruptedException e) {};
	
	
	}
	
	public synchronized void release() {
		notifyAll();
	}
}
 
Back
Top Bottom