1. Implement producer/Consumer mode: one to one alternate printing
public class MyService { private reentrantlock lock= new Reentrantlock (); Private Condition Condition = Lock.newcondition (); Private Boolean hasValue = false; public void Set () { try{ lock.lock (); while (HasValue = = True) { condition.await (); } SYSTEM.OUT.PRINTLN ("----"); HasValue = true; Condition.signal (); } catch (Interruptedexception e) { e.printstacktrace (); } finally { lock.unlock (); } } public void Get () { try { lock.lock (); while (HasValue = = False) { condition.await (); } SYSTEM.OUT.PRINTLN ("* * * *"); HasValue = false; Condition.signal (); } catch (Interruptedexception e) { e.printstacktrace (); } finally { lock.unlock ();}} }
Two Custom threads
public class MyThread1 extends Thread { private myservice service; Public MyThread1 (MyService service) { this.service = service; } @Override public Void Run () {for (int i=0; i<100; i++) { service.set ();}} }
public class MyThread2 extends thread{ private myservice service; Public MyThread2 (MyService service) { this.service = service; } @Override public Void Run () {for (int i=0; i<100; i++) { service.get ();}} }
public class Run {public static void Main (string[] args) throws interruptedexception { MyService service = new MyService (); MyThread1 a = new MyThread1 (service); A.start (); MyThread2 B = new MyThread2 (service); B.start (); }}
----
****
----
****
----
****
----
****
...... |
The result is alternate printing
---------------------------------------------------------------------------------------
2. Implement producer/Consumer mode: Many-to-many alternating printing
public class MyService {private Reentrantlock lock= new Reentrantlock (); Private Condition Condition = Lock.newcondition (); Private Boolean hasValue = false; public void Set () {try{lock.lock (); while (HasValue = = True) {System.out.println ("probable-continuous"); This is more than the above procedure condition.await (); } System.out.println ("----"); HasValue = true; Condition.signal (); } catch (Interruptedexception e) {e.printstacktrace (); } finally {Lock.unlock (); }} public void get () {try {lock.lock (); while (HasValue = = False) {System.out.println ("may be continuous");//It is more than the above program condition.await (); } System.out.println ("* * *"); HasValue = false; Condition.signal (); } catch (Interruptedexception e) {e.printstacktrace (); } finally {lock.unlock (); } }}Custom Thread ditto
Create a line Cheng
public class Run {public static void Main (string[] args) throws interruptedexception { MyService service = new My Service (); mythread1[] Threada = new MYTHREAD1[10]; mythread2[] threadb = new MYTHREAD2[10]; for (int i=0; i<10; i++) { Threada[i] = new MyThread1 (service); Threadb[i] = new MyThread2 (service); Threada[i].start (); Threadb[i].start ();}}}
----
It's possible--continuous
It's possible--continuous
****
It's possible. * Continuous
It's possible. * Continuous
It's possible. * Continuous
It's possible. * Continuous
It's possible. * Continuous
----
It's possible--continuous
It's possible--continuous
It's possible--continuous
It's possible--continuous
It's possible--continuous
...... |
Suspended animation after the program was run
According to the Notifyall () solution in chapter 3rd, you can use the Signalall () method to resolve. After you change two signal () codes in the Myservice.java class to Signalall (), the program runs correctly.
Logs printed from the console can be found, after the operation no longer appear suspended animation, suspended animation problem was solved.
The "----" and "* * * *" in the console are output alternately, but "it is possible that * * continuous" and "probable-continuous" are not alternating outputs, and sometimes continuous printing occurs. The reason is that a condition object is used in the program, and then the Signalall () method to wake up all the threads, then the wake-up thread is likely to be homogeneous, so there is a continuous printing "may be continuous" or "possible-continuous" situation
Java multithreaded Programming 4--lock example-implement producer/consumer mode: one-to-one, many-to-many alternating printing