File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes threading access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "threading access" Watch "threading access" New topic
Author

threading access

ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I saw this lesson on Sun's site but I can not figure out why the output is the way it is. There are 3 files, Producer, Consumer, and CubbyHole.
public class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number + " put: " + i); try {
sleep((int)(Math.random() * 100));
}
catch (InterruptedException e) { }
}
}
}
public class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
public class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
public static void main(String []args){
CubbyHole cb = new CubbyHole();
Producer p = new Producer(cb, 5);
Consumer c = new Consumer(cb, 5);
p.start();
c.start();
}
}
The output is:
Producer #5 put: 0
Consumer #5 got: 0
Producer #5 put: 1
Consumer #5 got: 1
Producer #5 put: 2
Producer #5 put: 3
Consumer #5 got: 2
Consumer #5 got: 3
Producer #5 put: 4
Consumer #5 got: 4
Producer #5 put: 5
Consumer #5 got: 5
Producer #5 put: 6
Consumer #5 got: 6
Producer #5 put: 7
Consumer #5 got: 7
Producer #5 put: 8
Consumer #5 got: 8
Producer #5 put: 9
Consumer #5 got: 9
Why did Consumer got 2 after Producer already put 3?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
I was wondering that myself. Here is my thought.
Since the System.out.println method is called outside of the synchronized method put or get, it may call them in a different order. So the first thing it does is call the put method, creates the lock, puts the value, notifies get, and then releases the lock. Now that it is realeased, get can start, doing it's thing. But since the print statement is outside of the put and get methods, it will depend on the scheduler on which thread will call the print method first. If we changed this code and put the print statement inside of the put and get synch methods, it would always write them in order:
Producer #5: put 1
Consmer #5: get 1
In order to do that you will have to change the construtor for the CubbyHole class and pass in a value. But you get the idea.
[This message has been edited by bill bozeman (edited October 18, 2000).]
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Thanks! BTW, does anyone know what the wait() actually does in get() and put(int)? I guess it releases the lock on the CubbyHole object, but I am not sure.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
You are right. When you say wait() in a method, this has to be in a synchronzied method or block. Since it is synchronized, that means there was a lock on the object. When you say wait(), you are telling the JVM that you want to release this lock and let other threads take over the lock. You combine this with a boolean flag so that you do not start your method again, until whatever else you needed to happen does and then you notify() your other thread that it can start again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threading access