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?