I believe this is similar to the dining philosophers problem but a little different and somewhat easier.

http://en.wikipedia.org/wiki/Dining_philosophers_problem

Basically I have a barrier class and a waitingThread class.

I need to create 4 threads. Each thread is meeting at a place to "eat". But none can begin "eating" until the last thread arrives, so they instead do separate things while waiting.
As each thread arrives I need to print out "I am thread 1(2, 3, or 4) and I am reading a book(doing homework, on my laptop ect.)"

When the 4th thread(last thread) arrives, that thread prints out "I am thread 4 and I am waiting to eat" followed by "I am thread 4 and I am eating" which I believe should be right after. As soon as thread 4 arrives, each thread will be allowed to start eating and thus will print out "I am thread 1 (2, 3, or 4) and I am eating"

So basically this is just creating some threads but having each thread wait at a certain point of execution until the last thread starts executing.

I'm brand new at thread programming as of this week and I've tried finding examples on the internet and there really are none on how to synchronize threads in this fashion using barriers. So this is what I have so far:

public class Barrier {
	private final int MAX;
	private int count;  //threads that have 'arrived' so far


	public Barrier (int max) {
	MAX = max;
	count = 0;
	}


	public synchronized void waitForThread() throws InterruptedException {
		count++;
	
if( count == MAX)
{
	//notify blocked threads that the last thread has arrived
	notifyAll();
}
else while (count < MAX)
{
	//Thread.sleep(1000);
	wait();
	
}


	}


}
public class waitingThread extends Thread{
	private Barrier barrier;
	private String  job; //Ex: reading a book, on laptop, doing homework
	private int     ID; 

	public waitingThread (Barrier b, String work, int threadID) {
	barrier = b;
	job    = work;
	ID      = threadID;

	}


	public void run () {
		if(id == 4)
		{
			System.out.println("I am thread " + ID + " and I am " + job);
		}
		else{
			System.out.println("I am thread " + ID + " I am working on " + job + " and I am waiting to eat");
		}
	
	
	
try {
//Thread.sleep(1000);
	barrier.waitForThread();
	
	System.out.println("I am thread " + ID + " and I am eating");
} catch (InterruptedException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}



	}


	public static void main (String [] args)throws InterruptedException {

	Barrier b = new Barrier (4); //4 threads must be synchronized

	// create waitingThread objects
waitingThread thread1 = new waitingThread(b, "doing homework", 1);
waitingThread thread2 = new waitingThread(b, "the laptop", 2);
waitingThread thread3 = new waitingThread(b, "reading a book", 3);
waitingThread thread4 = new waitingThread(b, "waiting", 4);


	// start them
thread1.start();
thread2.start();
thread3.start();
thread4.start();

	// join them
thread1.join();
thread2.join();
thread3.join();
thread4.join();

	} 

}

However when I run the code, the output fluctuates and changes.
One time it will look like this:
I am thread 1 I am working on doing homework and I am waiting to eat
I am thread 3 I am working on reading a book and I am waiting to eat
I am thread 2 I am working on the laptop and I am waiting to eat
I am thread 4 and I am waiting
I am thread 2 and I am eating
I am thread 1 and I am eating
I am thread 3 and I am eating
I am thread 4 and I am eating

Which looks right because the order of arrival of the threads doesn't matter except that they are all waiting on the 4th and last thread. So after thread 4 they should all begin eating.

However at other times it looks like this:
I am thread 2 I am working on the laptop and I am waiting to eat
I am thread 4 and I am waiting
I am thread 1 I am working on doing homework and I am waiting to eat
I am thread 3 I am working on reading a book and I am waiting to eat
I am thread 1 and I am eating
I am thread 3 and I am eating
I am thread 4 and I am eating
I am thread 2 and I am eating

And that is my problem. I'm not sure why or if the output is suppose to change and fluctuate like that. I am not sure if that is just due to the delay in System.out or if I am doing something wrong? I tried using Thread.sleep(1000) at various places and nothing is helping.

I'm brand new at thread programming and was hoping someone can help me out if I'm doing something wrong?

I think I figured it out, It was as simple as adding a thread.sleep() after starting each thread. Can't believe it took me this long to figure it out, thanks to the lag in System.out.println

So nothing was wrong, just the output got jumbled up by the computer if there's no delay. At least I think, that's the solution.

Check thread through isAlive Method... and Run another when first finishes.

There is a library in java to help with that. Try one of the classes here:

http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/package-summary.html

Specifically, try to see if these meet your needs:

CountDownLatch: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

CyclicBarrier: A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.

Semaphore: A counting semaphore.

Always Priority of Threads are 5(default). so which will execute first and second we cant guess it. Try to use setPriority() or Use Thread.sleep to make delay. So that you can see similar outputs for several execution.

Always Priority of Threads are 5(default). so which will execute first and second we cant guess it. Try to use setPriority() or Use Thread.sleep to make delay. So that you can see similar outputs for several execution.

Yeah, I just put thread.sleep(1000) in between each thread.start() call when starting each thread in main and the output was correct each time so I believe that fixed the problem. My original theory was that the system.out.print() for some threads was slower than the others even if it's by a few miliseconds since they are separate threads running and such so it would output the order differently even if it's running like its suppose to. I'm using Eclipse but I never tested it on a different compiler or in Linux to see if it does the same but I hope that's right since it seems to work. I guess I'll find out in a few days. I'm still new with thread programming in general.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.