"Not finished" communication between java--threads

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.