1. Multithreading - inter-thread communication - multi-producer multi-consumer issues
Multi-producer and multi-consumer. Wait for the wake mechanism.
Two questions were generated:
1. There have been many successive production, no consumption, or a commodity has been consumed several times.
Resolution: The token must be judged -------- every awakened thread, so the if judgment is changed to a while judgment.
2. There was a deadlock .
This party wakes up the party, causing all the threads to wait.
the solution is to wake up all the waiting threads. This awakens both the party and the other.
Although the problem of multi-production and consumption has been solved, some inefficiency.
Workaround One: Wake up all waiting threads
class resource{ private string name;//defines a product that has a name. private int count = 1; //defines the number of a product. private boolean flag = false;//defines a mark that is used to determine whether a product is available. public synchronized void set (String name) { while (flag) try{wait ();} catch (exception e) {}//t1,t2 this.name = name+ "-- "+count;//cake 1 cake 2 cake 3 count++; system.out.println ( Thread.CurrentThread (). GetName () + "---produced," +this.name); //production cake 1 production cake 2 production cake 3 flag = true; //change the token to true. notifyall ();//Wake up the waiting thread. } public synchronized void get ()//{ while (!flag) try{wait ();} catch (exception e) {}//t3.t4 system.out.println ( Thread.CurrentThread (). GetName () + "-------consumption ..." +this.name)//consumption cake 1 flag = false; notifyall (); }} //defines the task of the producer. class producer implements runnable{ private resource r; producer (resource r) { this.r = r; } public void run () { while (true) { r.set ("Cake"); } } } //defines the task of the consumer. class consumer implements runnable{ private resource r; consumer (resource r) { this.r = r; } public void run () { while (True) { R.get (); } }} class Threaddemo_producer_consumer2 { public stAtic void main (String[] args) { resource r = new resource (); producer pro = new producer (r); consumer con = new consumer (R); //creates two producer threads. thread t1 = new thread (PRO); thread t2 = new thread (PRO); //Create two consumer threads. thread t3 = new thread (Con); thread t4 = new thread (Con); t1.start (); &nBsp; t2.start (); t3.start (); t4.start (); }}
Workaround two: Use lock lock object and Condition Listener object
Creates a lock object based on the characteristics of the jdk1.5 version. More object-oriented than implicit lock operations that are synchronous. Provides a lock operation that is displayed.
Gets the monitor method object through the lock lock. Condition is responsible for the monitoring operation of the producer.
again creates a monitor method object. Responsible for consumer's monitoring operation.
Solve inefficient problems
import java.util.concurrent.locks.*; class resource{ //defines a product that has a name. private string name; //defines the number of a product. private int count = 1; //defines a mark that is used to determine whether a product is available. private boolean flag = false; //creates a lock object based on the features of the jdk1.5 version. More object-oriented than implicit lock operations that are synchronous. Provides a lock operation that is displayed. final lock lock = new reentrantlock ();//Mutex. //Gets the monitor method object through the lock lock. condition is responsible for the monitoring operation of the producer. final condition producer = lock.newcondition (); //Create a second monitor method object. Responsible for consumer's monitoring operation. final condition consumer = lock.newcondition (); public void set (String name)//{ //Gets the lock operation that is displayed through the lock object. lock.lock ();//Get the lock, the start position of the lock, where you want to lock the lock and place it try{ while (flag) try{producer.await ();} catch (exception e) {}//t1,t2 This.name = name+ "--" +count; count++; system.out.println (Thread.CurrentThread (). GetName () + "produced," + this.name); //changes the token to true. flag = true; consumer.signal ();//Wake up the waiting thread. }finally{ //release lock. lock.unlock ();//defined in Finally, the requirement must be released. } } Public void get ()//{ lock.lock (); try{ while (!flag) try{consumer.await () ;} catch (exception e) {}//t3.t4 System.out.println (Thread.CurrentThread(). GetName () + "--consumption." +this.name); //Consumption cake 1 flag = false; producer.signal (); }finally{ lock.unlock (); } }} //defines the task of the producer. class producer implements runnable{ private resource r; producer (resource r) { this.r = r; } public void run () { while (true) { &Nbsp; r.set ("Cake"); } } } //defines the task of the consumer. class consumer implements runnable{ private resource r; consumer (resource r) { this.r = r; } public void run () { while (True) { R.get (); } }} class threaddemo _producer_consumer3 { public static void main (String[] args) { resource r = new resource (); producer pro = new producer (R); &nbsP; consumer con = new consumer (R); //creates two producer threads. thread t1 = new thread (PRO); thread t2 = new thread (PRO); //Create two consumer threads. thread t3 = new thread (Con); thread t4 = new thread (Con); t1.start (); t2.start (); t3.start (); t4.start (); }}
Multithreading-Inter-thread communication-multi-producer multi-consumer example