No communication between producers and consumers:
1 2 classsynchronizedmethods{3 intD;4 synchronized voidgetDate () {5System.out.println ("Got data:" +d);6 }7 synchronized voidPutData (intd) {8 This. d=D;9System.out.println ("Put data:" +d);Ten } One } A classProducerextendsthread{ - Synchronizedmethods t; - PublicProducer (Synchronizedmethods t) { the This. t=T; - } - Public voidrun () { - intdata=700; + while(true ) { -System.out.println ("Put called by producer"); +T.putdata (data++); A } at } - - } - classConsumerextendsThread { - Synchronizedmethods t; - PublicConsumer (Synchronizedmethods t) { in This. t=T; - } to Public voidrun () { + while(true) { -System.out.println ("Get called by Consumer"); the t.getdate (); * } $ }Panax Notoginseng } - Public classProducerconsumer { the Public Static voidMain (String args[]) { +Synchronizedmethods obj1=Newsynchronizedmethods (); AProducer p=NewProducer (obj1); theProducer c=NewProducer (obj1); + P.start (); - C.start (); $ } $}View Code
Infinite loops
With the communication between producer and consumer true:
"Not finished" communication between java--threads