Jump to content

(java)How does this thread behave?


albertlee

Recommended Posts

CubbyHole class

 

public class CubbyHole {
Object item;

public synchronized Object get()
		throws InterruptedException {
	while(item == null)
		wait();

	Object i = item;
	item = null;
	notifyAll();
	return i;
}

public synchronized void put(Object i)
		throws InterruptedException {
	while(item != null)
		wait();

	item = i;
	notifyAll();
}
}

 

and

 

TestCubbyHole

 

public class TestCubbyHole {

public static void main(String[] args) {
	Thread mainThread = Thread.currentThread();

	CubbyHole c = new CubbyHole();

	new CubbyWatcher(1, c);
	new CubbyWatcher(2, c);
	new CubbyWatcher(3, c);
	new CubbyWatcher(4, c);
	new CubbyWatcher(5, c);
	new CubbyWatcher(6, c);

	for(int ii=0 ; ii<args.length ; ii++) {
		new CubbyFiller(args[ii], c);
	}

	try {
		System.in.read();
	} catch (java.io.IOException ioe) {
	}
}

static class CubbyWatcher implements Runnable {
	int id;
	CubbyHole theCubbyHole;

	public CubbyWatcher(int id, CubbyHole c) {
		this.id = id;
		theCubbyHole = c;
		System.out.println("CubbyWatcher");

		Thread t = new Thread(this);
		t.setDaemon(true);
		t.start();
	}

	public void run() {
		while(true) {
			try {
				Object o = theCubbyHole.get();
				System.out.println("Watcher #" + id +
						" got: " + o);

			} catch (InterruptedException ie) {
			}
		}
	}
}

static class CubbyFiller implements Runnable {
	Object item;
	CubbyHole theCubbyHole;

	public CubbyFiller(Object i, CubbyHole c) {
		item = i;
		theCubbyHole = c;
		System.out.println("CubbyFiller" + ", item = " + item);

		Thread t = new Thread(this);
		t.setDaemon(true);
		t.start();
	}

	public void run() {
		Thread me = Thread.currentThread();
		[b][i]//int waitTime = (int)(Math.random() * 1000);[/i][/b]

		try {
			[b][i]//me.sleep(waitTime);[/b][/i]
			theCubbyHole.put(item);
		} catch (InterruptedException ie) {
		}
	}
}
}

 

In TestCubbyHole's CubbyFiller's run() method, if let the me.sleep uncommented, the output will be something like

 

parameter: aaa bbb ccc ddd eee

CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyFiller, item = aaa
CubbyFiller, item = bbb
CubbyFiller, item = ccc
CubbyFiller, item = ddd
CubbyFiller, item = eee
Watcher #1 got: ddd
Watcher #2 got: ccc
Watcher #3 got: bbb
Watcher #4 got: aaa
Watcher #5 got: eee

 

 

where the last 4 lines (Watcher #s) are quite random. I understand that.

 

However, if with the code lines commented, I expect the out put to be:

 

 

CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyFiller, item = aaa
CubbyFiller, item = bbb
CubbyFiller, item = ccc
CubbyFiller, item = ddd
CubbyFiller, item = eee
Watcher #1 got: aaa
Watcher #2 got: bbb
Watcher #3 got: ccc
Watcher #4 got: ddd
Watcher #5 got: eee

 

but however in fact it is:

 

CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyFiller, item = aaa
CubbyFiller, item = bbb
CubbyFiller, item = ccc
Watcher #1 got: aaa
CubbyFiller, item = ddd
CubbyFiller, item = eee
Watcher #2 got: bbb
Watcher #3 got: ccc
Watcher #4 got: ddd
Watcher #5 got: eee

 

Can any one explain why??

 

thanks">

Link to comment
Share on other sites

Ok, my understanding of this is minimal but just a question -

 

As the CubbyWatcher threads have been created, they are running away in the background quite happily, and then you create the CubbyFiller thread/object which in the example your referring to almost immediately "puts" the value. As the value has been "put()", "available" for that CubbyFillers CubbyHole has been set to true and "notifyAll()" has been run so the "wait()" will trip out and if we are for instance talking about the 1st CubbyFiller/Hole, then the first CubbyWatcher's "get()" will finally work and it will output. So it looks to me anyways that CubbyWatcher's wouldn't have to be after all the CubbyFiller output, only after their specific CubbyFiller output and in theory if the processing for the CubbyWatcher was small enough and the job management worked out nicely you could in theory end up with -

 

CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyWatcher
CubbyFiller, item = aaa
Watcher #1 got: aaa
CubbyFiller, item = bbb
Watcher #2 got: bbb
CubbyFiller, item = ccc
Watcher #3 got: ccc
CubbyFiller, item = ddd
Watcher #4 got: ddd
CubbyFiller, item = eee
Watcher #5 got: eee

 

But as the CubbyWatcher processing takes some time, 2 more (in the example you showed) CubbyFillers and therefore CubbyHoles have been created. So if it takes roughly 2 CubbyFiller creation "times" to finalise the CubbyWatchers output, the next CubbyWatcher comes out after "e" which it does in this example. I would draw a diagram to show what I mean but i can't draw diagrams to save my life.

 

At least thats the way it seems to me but you probably know far more about this than me (ive only played about with threads and never really done much with them) and I may be grossly misunderstanding what is going on. Those are just my 2 cents and my question to you is, does what I am saying make sense to you from what youve been doing?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.