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 ();}}