In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains how to use the combination of ReentrantLock class and Condition class in jdk. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the use of the combination of ReentrantLock class and Condition class in jdk"!
In the previous article, we briefly introduced the basic use of the ReentrantLock class, that is, to acquire and release locks. What should we do if we want to implement wait () and notify () waiting for notification in the ReentrantLock class. At this point we are going to use a new class, which is the Condition class. The Condition class is also a new class added to JDK1.5 later. It can achieve multi-notification function, that is to say, multiple Condition classes can be created in a Lock object is equivalent to multiple lock objects, notification can be selective thread notification, rather than notify () that CPU randomly decides which thread to notify, the Condition class makes it more flexible in thread scheduling. Let's take a look at specific examples.
/ * *
* user login
*
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-15 10:35
* @ since 1.0.0
, /
Public class Userinfo {
Private Lock lock = new ReentrantLock ()
Private Condition condition = lock.newCondition ()
Public void await () {
Try {
Lock.lock ()
System.out.println (String.format ("thread start\ tthread:% s", Thread.currentThread () .getName ()
Condition.await ()
System.out.println (String.format ("thread end\ tthread:% s", Thread.currentThread () .getName ()
} catch (InterruptedException e) {
E.printStackTrace ()
} finally {
Lock.unlock ()
}
}
Public void signal () {
Lock.lock ()
System.out.println (String.format ("thread recovery\ tthread:% s", Thread.currentThread () .getName ()
Condition.signal ()
Lock.unlock ()
}
}
/ * *
* manage user requests
*
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-15 10:44
* @ since 1.0.0
, /
Public class RequestAdmin extends Thread {
Private Userinfo userinfo
Public RequestAdmin (Userinfo userinfo) {
This.userinfo = userinfo
}
@ Override
Public void run () {
Userinfo.await ()
}
} / * *
* user request
*
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-15 10:44
* @ since 1.0.0
, /
Public class RequestUser extends Thread {
Private Userinfo userinfo
Public RequestUser (Userinfo userinfo) {
This.userinfo = userinfo
}
@ Override
Public void run () {
Userinfo.signal ()
}
} / * *
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-20 13:35
* @ since 1.0.0
, /
Public class Test {
Public static void main (String [] args) throws InterruptedException {
Userinfo userinfo = new Userinfo ()
RequestAdmin requestAdmin = new RequestAdmin (userinfo)
RequestUser requestUser = new RequestUser (userinfo)
RequestAdmin.start ()
Thread.sleep (1000)
For (int I = 3; I > 0; imuri -) {
System.out.println (String.format ("countdown:% s", I))
Thread.sleep (1000)
}
RequestUser.start ()
}
} Thread start thread: Thread-0
Countdown: 3
Countdown: 2
Countdown: 1
Thread recovery thread: Thread-1
Thread end thread: Thread-0
We successfully implemented the wait () and notify () pause and notification functions with the ReentrantLock and Condition classes. It is still relatively easy to use, basically consistent with the previous methods of wait () and notify ().
Condition.await (); / / equivalent to wait () method condition.signal (); / / equivalent to notify () method
We know that you can restore all paused threads with the notifyAll () method, and there is the same method in the Condition class that implements this logic, except that the method is not called notifyAll () but signalAll (). Please look at the following example.
Import java.util.concurrent.locks.Condition
Import java.util.concurrent.locks.Lock
Import java.util.concurrent.locks.ReentrantLock
/ * *
* user login
*
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-15 10:35
* @ since 1.0.0
, /
Public class Userinfo {
Private Lock lock = new ReentrantLock ()
Private Condition condition = lock.newCondition ()
Public void await1 () {
Try {
Lock.lock ()
System.out.println (String.format ("await1 thread start\ tthread:% s", Thread.currentThread () .getName ()
Condition.await ()
System.out.println (String.format ("Await1 thread end\ tthread:% s", Thread.currentThread () .getName ()
} catch (InterruptedException e) {
E.printStackTrace ()
} finally {
Lock.unlock ()
}
}
Public void await2 () {
Try {
Lock.lock ()
System.out.println (String.format ("await2 thread start\ tthread:% s", Thread.currentThread () .getName ()
Condition.await ()
System.out.println (String.format ("await2 thread end\ tthread:% s", Thread.currentThread () .getName ()
} catch (InterruptedException e) {
E.printStackTrace ()
} finally {
Lock.unlock ()
}
}
Public void signalAll () {
Lock.lock ()
System.out.println (String.format ("restore all threads\ tthread:% s", Thread.currentThread () .getName ()
Condition.signalAll ()
Lock.unlock ()
}
} / * *
* manage user requests
*
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-15 10:44
* @ since 1.0.0
, /
Public class RequestAdmin extends Thread {
Private Userinfo userinfo
Public RequestAdmin (Userinfo userinfo) {
This.userinfo = userinfo
}
@ Override
Public void run () {
Userinfo.await1 ()
}
} / * *
* user request
*
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-15 10:44
* @ since 1.0.0
, /
Public class RequestUser extends Thread {
Private Userinfo userinfo
Public RequestUser (Userinfo userinfo) {
This.userinfo = userinfo
}
@ Override
Public void run () {
Userinfo.await2 ()
}
} / * *
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-20 13:35
* @ since 1.0.0
, /
Public class Test {
Public static void main (String [] args) throws InterruptedException {
Userinfo userinfo = new Userinfo ()
RequestAdmin requestAdmin = new RequestAdmin (userinfo)
RequestUser requestUser = new RequestUser (userinfo)
RequestAdmin.start ()
RequestUser.start ()
Thread.sleep (1000)
For (int I = 3; I > 0; imuri -) {
System.out.println (String.format ("countdown:% s", I))
Thread.sleep (1000)
}
Userinfo.signalAll ()
}
} await1 thread starts thread: Thread-0
Await2 thread starts thread: Thread-1
Countdown: 3
Countdown: 2
Countdown: 1
Restore all threads thread: main
Await1 thread ends thread: Thread-0
Await2 thread ends thread: Thread-1
We see that all paused threads are restored because of the call to the signalAll () method. This is the same as the notifyAll () method, there is nothing to say. But what if we want to restore the specified thread? We know that it is up to CPU to decide which thread to restore by calling the notifyAll () method, and our program cannot control it. At this point, someone may think of setting the priority of the thread to give priority to the specified thread. But that's not absolute. Setting the priority also means that the thread has a high probability of getting execution, and there is no guarantee of 100% execution. Although there is no easy way to resume execution of the specified thread with the notifyAll () method, this function can be easily implemented in the Condition class, which is also the embodiment of the multi-channel notification function of the Condition class. Let's demonstrate how to implement our above requirements with the Condition class.
Import java.util.concurrent.locks.Condition
Import java.util.concurrent.locks.Lock
Import java.util.concurrent.locks.ReentrantLock
/ * *
* user login
*
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-15 10:35
* @ since 1.0.0
, /
Public class Userinfo {
Private Lock lock = new ReentrantLock ()
Private Condition condition1 = lock.newCondition ()
Private Condition condition2 = lock.newCondition ()
Public void await1 () {
Try {
Lock.lock ()
System.out.println (String.format ("await1 thread start\ tthread:% s", Thread.currentThread () .getName ()
Condition1.await ()
System.out.println (String.format ("Await1 thread end\ tthread:% s", Thread.currentThread () .getName ()
} catch (InterruptedException e) {
E.printStackTrace ()
} finally {
Lock.unlock ()
}
}
Public void await2 () {
Try {
Lock.lock ()
System.out.println (String.format ("await2 thread start\ tthread:% s", Thread.currentThread () .getName ()
Condition2.await ()
System.out.println (String.format ("await2 thread end\ tthread:% s", Thread.currentThread () .getName ()
} catch (InterruptedException e) {
E.printStackTrace ()
} finally {
Lock.unlock ()
}
}
Public void signalAll1 () {
Lock.lock ()
System.out.println (String.format ("restore all threads\ tthread:% s", Thread.currentThread () .getName ()
Condition1.signalAll ()
Lock.unlock ()
}
Public void signalAll2 () {
Lock.lock ()
System.out.println (String.format ("restore all threads\ tthread:% s", Thread.currentThread () .getName ()
Condition2.signalAll ()
Lock.unlock ()
}
} / * *
* @ author Sama
* @ author admin@jilinwula.com
* @ date 2017-03-20 13:35
* @ since 1.0.0
, /
Public class Test {
Public static void main (String [] args) throws InterruptedException {
Userinfo userinfo = new Userinfo ()
RequestAdmin requestAdmin = new RequestAdmin (userinfo)
RequestUser requestUser = new RequestUser (userinfo)
RequestAdmin.start ()
RequestUser.start ()
Thread.sleep (1000)
For (int I = 3; I > 0; imuri -) {
System.out.println (String.format ("countdown:% s", I))
Thread.sleep (1000)
}
Userinfo.signalAll1 ()
}
} await1 thread starts thread: Thread-0
Await2 thread starts thread: Thread-1
Countdown: 3
Countdown: 2
Countdown: 1
Restore all threads thread: main
Await1 thread ends thread: Thread-0
We see that at this time our above-mentioned needs have been realized. Using the Condition class to wake up our specified thread is really simpler and more convenient than notify ().
At this point, I believe you have a deeper understanding of the use of the combination of ReentrantLock class and Condition class in jdk, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.