JAVA multithreading uses Lock,Condition to realize the sequential execution of A _ Magi B _ pencil C _ D, how to achieve exclusion and synchronous communication.
This article mainly introduces "JAVA multithreading uses Lock,Condition to realize the sequential execution of AMagneBPeragence D, how to achieve exclusivity and synchronous communication". In daily operation, I believe many people use Lock,Condition to realize the sequential execution of AMagne BMagint C Personality in JAVA multithreading. There are doubts about how to achieve exclusive and synchronous communication. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. Hope to answer the question of "JAVA multithreading using Lock,Condition to achieve A _ Magi B ~ C ~ D execution in turn, how to achieve exclusive, synchronous communication" is helpful! Next, please follow the editor to study!
Package com.study;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class BlockingQueueDemo {public static void main (String [] args) {BlockingQueueDemo blockingQueueDemo = new BlockingQueueDemo (); final BlockingQueueClass blockingQueueClass = blockingQueueDemo.new BlockingQueueClass (); Thread thread = new Thread (new Runnable () {@ Override public void run () {while (true) {blockingQueueClass.invokeA () ); thread.start (); Thread thread2 = new Thread (new Runnable () {@ Override public void run () {while (true) {blockingQueueClass.invokeB ();}); thread2.start () Thread thread3 = new Thread (new Runnable () {@ Override public void run () {while (true) {blockingQueueClass.invokeC ();}); thread3.start (); Thread thread4 = new Thread (new Runnable () {@ Override public void run () {while (true) {blockingQueueClass.invokeD () }); thread4.start ();} class BlockingQueueClass {Lock lock = new ReentrantLock (); Condition conditionA = lock.newCondition (); Condition conditionB = lock.newCondition (); Condition conditionC = lock.newCondition (); Condition conditionD = lock.newCondition (); boolean syncA = true; boolean syncB = false; boolean syncC = false; boolean syncD = false; public void invokeA () {lock.lock () Try {while (! syncA) {try {conditionA.await ();} catch (InterruptedException e) {e.printStackTrace ();}} syncA = false; syncB = true; System.out.println ("invoke A...."); conditionB.signal () } catch (Exception e) {e.printStackTrace ();} finally {lock.unlock ();}} public void invokeB () {lock.lock (); try {while (! syncB) {try {conditionB.await ();} catch (InterruptedException e) {e.printStackTrace () } syncB = false; syncC = true; System.out.println ("invoke B...."); conditionC.signal ();} catch (Exception e) {e.printStackTrace ();} finally {lock.unlock ();}} public void invokeC () {lock.lock () Try {while (! syncC) {try {conditionC.await ();} catch (InterruptedException e) {e.printStackTrace ();}} syncC = false; syncD = true; System.out.println ("invoke C."); conditionD.signal () } catch (Exception e) {e.printStackTrace ();} finally {lock.unlock ();}} public void invokeD () {lock.lock (); try {while (! syncD) {try {conditionD.await ();} catch (InterruptedException e) {e.printStackTrace () }} syncD = false; syncA = true; System.out.println ("invoke D...."); conditionA.signal ();} catch (Exception e) {e.printStackTrace ();} finally {lock.unlock () At this point, the study on "JAVA multithreading uses Lock,Condition to achieve the implementation of AMagi Bpomery D in turn, how to achieve exclusive, synchronous communication" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!