Producer Consumer

I try this code

publicclass ProducerConsumerTest{

publicstaticvoid main(String[] args){

CubbyHole c =new CubbyHole();

Producer p1 =new Producer(c, 1);

Consumer c1 =new Consumer(c, 1);

p1.start();

c1.start();

}

}

class CubbyHole{

privateint contents;

privatevolatileboolean available =false;

publicsynchronizedint get(){

while (available ==false){

try{

wait();

}catch (InterruptedException e){

}

}

available =false;

notify();

return contents;

}

publicsynchronizedvoid put(int value){

while (available ==true){

try{

wait();

}catch (InterruptedException e){

}

}

contents = value;

available =true;

notify();

}

}

class Consumerextends Thread{

private CubbyHole cubbyhole;

privateint number;

public Consumer(CubbyHole c,int number){

cubbyhole = c;

this.number = number;

}

publicvoid run(){

int value = 0;

for (int i = 0; i < 10; i++){

value = cubbyhole.get();

System.out.println("Consumer #" + this.number +" got: " + value);

}

}

}

class Producerextends Thread{

private CubbyHole cubbyhole;

privateint number;

public Producer(CubbyHole c,int number){

cubbyhole = c;

this.number = number;

}

publicvoid 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){

}

}

}

}

from this link http://www.java2s.com/Code/Java/Threads/ProducerConsumerTest.htm

but, why can I get this result?

Producer #1 put: 0

Consumer #1 got: 0

Consumer #1 got: 1

Producer #1 put: 1

Consumer #1 got: 2

Producer #1 put: 2

Consumer #1 got: 3

Producer #1 put: 3

Producer #1 put: 4

Consumer #1 got: 4

Consumer #1 got: 5

Producer #1 put: 5

Consumer #1 got: 6

Producer #1 put: 6

Consumer #1 got: 7

Producer #1 put: 7

Consumer #1 got: 8

Producer #1 put: 8

Consumer #1 got: 9

Producer #1 put: 9

somebody can explain it to me?

if I set the priority higher in Producer, I get the right answer.

thanks

[5652 byte] By [Bina_Nusantaraa] at [2007-11-26 16:19:04]
# 1
Bina,Are you asking how something could appear to be consumed before it is produced?Consumer #1 got: 2Producer #1 put: 2Keith.
corlettka at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 2

Note that those System.out.println()s run non-synchronized so any

tuple of single got/put lines can appear in any order. Also note that you

can never read, say, "got 1, got 2, got 3, put 1, put 2 ..." because the

other methods are properly synchronize, i.e. you can't get something

before it's put and you can't put anything before the previous one is gotten.

kind regards,

Jos

JosAHa at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 3

Jos,

Please, do you know does System.out & System.err have a mechanism to prevent interleaving of output from multiple threads? ... and would you perchance know how that mechism is implemented... semaphore, thread lock, file system lock... or something I've never heard of.

Ta. Keith.

corlettka at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 4

public void run() {

int value = 0;

for (int i = 0; i < 10; i++) {

value = cubbyhole.get();

System.out.println("Consumer #" + this.number + " got: " + value);

}

}

I know what do you mean, but what must i do if i still want to print the result in void run ? How can i synchronize System.out.println ?

Bina_Nusantaraa at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 5

public void run() {

int value = 0;

for (int i = 0; i < 10; i++) {

value = cubbyhole.get();

System.out.println("Consumer #" + this.number + " got: " + value);

}

}

I know what do you mean, but what must i do if i still want to print the result in void run ? How can i synchronize System.out.println ?

Bina_Nusantaraa at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 6

> I know what do you mean, but what must i do if i still want to print the

> result in void run ? How can i synchronize System.out.println ?

You can't. You have to move those System.out.println()s over to the

cubbyhole's put() and get() methods then, where the prints() will be

automatically synchronized.

kind regards,

Jos

JosAHa at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 7

> Please, do you know does System.out & System.err have

> a mechanism to prevent interleaving of output from

> multiple threads? ... and would you perchance know

> how that mechism is implemented... semaphore, thread

> lock, file system lock... or something I've never heard of.

AFAIK nothing prevents interleaved output; at least not in the Java part

(I just checked the System class source). The actual flushing to the

native part is conducted by JNI code and the OS and is out of control of

the JVM. I also can't find any special OutputStreams either in the Java

part of this all.

kind regards,

Jos

JosAHa at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 8
thank you for your answer
Bina_Nusantaraa at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 9

> Jos,

>

> Please, do you know does System.out & System.err have

> a mechanism to prevent interleaving of output from

> multiple threads? ... and would you perchance know

> how that mechism is implemented... semaphore, thread

> lock, file system lock... or something I've never

> heard of.

>

> Ta. Keith.

Jos... please?

corlettka at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 10
> Jos... please?See reply #7; our replies crossed ;-)kind regards,Jos
JosAHa at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...
# 11
> See reply #7; our replies crossed ;-)> kind regards,> Josthank you for your answer :-)
corlettka at 2007-7-8 22:42:24 > top of Java-index,Java Essentials,New To Java...