Producer-Consumer model
1. Example:
Class Resource{private string Name;private int count = 1;private Boolean flag = false;public synchronized void Set (string Name) {if (flag) try {this.wait ();//this represents the calling function thread} catch (Interruptedexception e) {}this.name = name+ "__" + count++; System.out.println (Thread.CurrentThread (). GetName () + "... Producer "+ this.name); flag = True;this.notifyall ();} Public synchronized void Show () {if (!flag) try {this.wait ();} catch (Interruptedexception e) {}system.out.println ( Thread.CurrentThread (). GetName () + "consumer" + this.name); flag = False;this.notifyall ();}} Define production class Producter, produce commodity class Producter implements Runnable{private Resource R; Producter (Resource r) {THIS.R = R;} public void Run () {while (true) {R.set ("Commodity");}}} Define consumer class customer, consumer goods class customer implements Runnable{private Resource R; Customer (Resource r) {THIS.R = R;} public void Run () {while (true) {R.show ()}}}
Analysis:
- Multiple producers, consumers define the use while Judgment flag
Reason: Let the awakened thread judge the token again
- Using Notifyall in Definitions
Cause: Because of the need to wake up the other thread, if using notify is prone to wake up the local thread, all threads in the thread are in a wait state
Multi-threaded solution available in 2.jdk5
- Replace the sync synchronized with the lock operation
- Replace Wait/notify/notifyall in object with Condtion object
- The object can be obtained through a lock lock
Example:
Package Unit18;import java.util.concurrent.locks.*;class resources{private String name;private int count = 1;private Boolean flag = false;private Lock lock = new Reentrantlock ();//define Lock class private Condition Condition_pro = Lock.newcondition ( );p rivate Condition Condition_con = lock.newcondition ();p ublic void Set (String name) throws interruptedexception{ Lock.lock ();//Add lock Try{while (flag) {condition_pro.wait ();//producer call thread wait}this.name = name+ "__" + count++; System.out.println (Thread.CurrentThread (). GetName () + "... Producer "+ this.name); flag = True;condition_pro.signal ();//wake-up consumer thread}catch (Exception e) {}finally{lock.unlock ();//Unlock}} Public synchronized void Show () throws Interruptedexception{lock.lock (); Try{while (!flag) {condition_con.wait ();} System.out.println (Thread.CurrentThread (). GetName () + "consumer" + this.name); flag = False;condition_con.signal ();// Wake-Up producer thread}catch (Exception e) {}finally{lock.unlock ();}}} Class Producters implements Runnable{private Resources R; Producters (Resources r) {THIS.R = R;} public voidRun () {while (true) {try {r.set ("Commodity");} catch (Interruptedexception e) {}}}}class Customers implements Runnable{private Resources R; Customers (Resources r) {THIS.R = R;} public void Run () {while (true) {try {r.show ()} catch (Interruptedexception e) {}}}}public class ProducterCustomerTest2 { public static void Main (string[] args) {Resources R = new resources (); Producters Pro = new Producters (r); Customers cus = new Customers (r); thread T1 = new Thread (PRO),//thread t2 = new Thread (PRO),//thread t3 = new Thread (cus); Thread t2 = new Thread (cus); T1.start ();//t2.start ();//t3.start (); T2.start ();}}
Producer-Consumer model