Java basic review: thread communication and producer consumer

Source: Internet
Author: User

Thread communication C-P exercise: the bucket stores "name, gender" type of data, the producer is responsible for putting data to the bucket, the consumer is responsible for getting data from the bucket. Program to realize the C-P relationship. And implement synchronous communication between threads.

1) Synchronous Code block for synchronous communication

Package day14;/*** data Storage area, and set the default production content ** @ author well **/class Storage {String name = "Tom "; string sex = "male";}/*** Producer process ** @ author well **/class Producer implements Runnable {Storage s = null; Producer (Storage s) {super (); this. s = s ;}@ Overridepublic void run () {int I = 0; for (; I <500; I ++) {synchronized (s) {if (I % 2 = 0) {s. name = "Lily"; s. sex = "female";} else {s. name = "Tom"; s. sex = "male" ;}}}}/*** Consumer process ** @ author well **/class Consumer implements Runnable {Storage s = null; Consumer (Storage s) {super (); this. s = s ;}@ Overridepublic void run () {for (int I = 0; I <500; I ++) {synchronized (s) {System. out. println (s. name + "," + s. sex) ;}}} public class PCDemo {public static void main (String [] args) {Storage s = new Storage (); new Thread (new Producer (s )). start (); new Thread (new Consumer (s )). start ();}}

 

2) Implement the synchronization method and improve the readability and organization of the Code.

Package day14;/*** data Storage area, and set the default production content ** @ author well **/class Storage {String name = "Tom "; string sex = "male";/*** put data ** @ param name * @ param sex */public synchronized void put (String name, String sex) {this. name = name; try {Thread. sleep (10);} catch (InterruptedException e) {e. printStackTrace ();} this. sex = sex;}/*** get data */public synchronized void get () {System. out. println (name + "," + sex) ;}/ *** Producer process ** @ author well **/class Producer implements Runnable {Storage s = null; producer (Storage s) {super (); this. s = s ;}@ Overridepublic void run () {int I = 0; for (; I <500; I ++) {if (I % 2 = 0) {s. put ("Lily", "female");} else {s. put ("Tom", "male") ;}}}/*** Consumer process ** @ author well **/class Consumer implements Runnable {Storage s = null; consumer (Storage s) {super (); this. s = s ;}@ Overridepublic void run () {for (int I = 0; I <500; I ++) {s. get () ;}} public class PCDemo {public static void main (String [] args) {Storage s = new Storage (); new Thread (new Producer (s )). start (); new Thread (new Consumer (s )). start ();}}

 

3) implement with reentrant locks

Package day14; import java. util. concurrent. locks. reentrantLock;/*** data Storage area, and set the default production content ** @ author well **/class Storage {private String name = "Tom "; private String sex = "male"; private ReentrantLock lock = new ReentrantLock (); /*** put data ** @ param name * @ param sex */public void put (String name, String sex) {lock. lock (); this. name = name; try {Thread. sleep (10); this. sex = sex;} catch (InterruptedException e) {e. printStackTrace ();} finally {lock. unlock () ;}/ *** fetch data */public synchronized void get () {lock. lock (); try {System. out. println (name + "," + sex);} finally {lock. unlock () ;}}/ *** Producer process ** @ author well **/class Producer implements Runnable {Storage s = null; Producer (Storage s) {super (); this. s = s ;}@ Overridepublic void run () {int I = 0; for (; I <500; I ++) {if (I % 2 = 0) {s. put ("Lily", "female");} else {s. put ("Tom", "male") ;}}}/*** Consumer process ** @ author well **/class Consumer implements Runnable {Storage s = null; consumer (Storage s) {super (); this. s = s ;}@ Overridepublic void run () {for (int I = 0; I <500; I ++) {s. get () ;}} public class PCDemo {public static void main (String [] args) {Storage s = new Storage (); new Thread (new Producer (s )). start (); new Thread (new Consumer (s )). start ();}}

 

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.