In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Java how to achieve multi-threaded sequential execution, the article is very detailed, has a certain reference value, interested friends must read it!
Scene
Write a program and start three threads. The name of the three threads is A _ Magi B ~ C _ TX. Each thread prints its own ID value on the screen five times, in the order of ABCABC....
Use synchronized to implement public class MyService {private int flag = 1; public synchronized void printA () {while (flag! = 1) {try {this.wait ();} catch (InterruptedException e) {e.printStackTrace () }} System.out.print (Thread.currentThread (). GetName ()); flag = 2; this.notifyAll ();} public synchronized void printB () {while (flag! = 2) {try {this.wait () } catch (InterruptedException e) {e.printStackTrace ();}} System.out.print (Thread.currentThread (). GetName ()); flag = 3; this.notifyAll () } public synchronized void printC () {while (flag! = 3) {try {this.wait ();} catch (InterruptedException e) {e.printStackTrace ();}} System.out.print (Thread.currentThread () .getName ()) Flag = 1; this.notifyAll ();}}
What's the difference between using while instead of if in the judgment condition here? The thread wakes up from the wait state and continues to execute after acquiring the lock, for example, A calls nofityAll () to wake up BMagazine C, in which case it is uncertain which B or C will acquire the lock first. If C acquires the lock first, then C continues to print, which is not in line with our expectations. So here we use a while, when C gets the lock and then determines the flag, and if it's not time for it to execute, it enters the wait state again. At this time, both An and C are wait states, and it must be B who gets the lock, so that we can print in the desired order.
The test class package testABC;public class TestMain {public static void main (String [] args) {/ / write a program to start three threads, and the ID of the three threads is respectively Amam Benerc. Each thread prints its own ID value on the screen five times, in the order ABCABC...// MyService service = new MyService (); MyService2 service = new MyService2 (); Thread A = new Thread (new Runnable () {@ Override public void run () {for (int I = 0; I < 5) Thread +) {service.printA ()}); A.setName ("A"); Thread B = new Thread (new Runnable () {@ Override public void run () {for (int I = 0; I < 5) Thread +) {service.printB ()}); B.setName ("B"); Thread C = new Thread (new Runnable () {@ Override public void run () {for (int I = 0; I < 5) Service.printC +) {service.printC ();}); C.setName ("C"); A.start (); B.start (); C.start ();}} use Lock to implement import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock Import java.util.concurrent.locks.ReentrantLock;public class MyService2 {private int flag = 1; private Lock lock = new ReentrantLock (); private Condition conditionA = lock.newCondition (); private Condition conditionB = lock.newCondition (); private Condition conditionC = lock.newCondition (); public void printA () {try {lock.lock () If (flag! = 1) {try {conditionA.await ();} catch (InterruptedException e) {e.printStackTrace () }} System.out.print (Thread.currentThread (). GetName ()); flag = 2; conditionB.signal ();} finally {lock.unlock ();}} public void printB () {try {lock.lock () If (flag! = 2) {try {conditionB.await ();} catch (InterruptedException e) {e.printStackTrace () }} System.out.print (Thread.currentThread (). GetName ()); flag = 3; conditionC.signal ();} finally {lock.unlock ();}} public void printC () {try {lock.lock () If (flag! = 3) {try {conditionC.await ();} catch (InterruptedException e) {e.printStackTrace () }} System.out.print (Thread.currentThread (). GetName ()); flag = 1; conditionA.signal ();} finally {lock.unlock ();}
You don't have to use while when using LOCK because condition wakes up a specified thread. Also note that conditionA.signal () must be called first, and then lock.unlock ();, otherwise a java.lang.IllegalMonitorStateException exception will be thrown. Because after calling unlock, the current thread is no longer the holder of this monitor object condition. That is, the locked object can only be used when this thread holds it.
Blog post about this exception: about java.lang.IllegalMonitorStateException
Interpretation in api
Public class IllegalMonitorStateExceptionextends RuntimeException
The exception thrown indicates that a thread has tried to wait for the monitor of the object, or to notify other threads that are waiting for the object without specifying the monitor itself.
Start with the following versions:
JDK1.0
See also:
Object.notify (), Object.notifyAll (), Object.wait (), Object.wait (long), Object.wait (long, int), serialization table
That is, the current thread is not the owner of this object monitor. That is, you have to lock the object in the current thread before you can use the locked object. These methods need to use synchronized, and the object will be executed with whatever object is locked.
Notify (), notifyAll (), wait (), wait (long), wait (long, int) operation, otherwise an IllegalMonitorStateException exception will be reported.
For example:
Exapmle 1, the instance object to which the locking method belongs:
Public synchronized void method () {/ / then you can call: this.notify (). / / or call notify () directly.}
Exapmle 2, the Class of the instance to which the locking method belongs:
Public Class Test {public static synchronized void method () {/ / then call: Test.class.notify ().. }}
Exapmle 3, lock other objects:
Public Class Test {public Object lock = new Object (); public static void method () {synchronized (lock) {/ / need to call lock.notify ();} above is all the content of the article "how to implement multithreaded sequential execution by Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.