In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the Java concurrent Condition example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Before using Lock, the most commonly used synchronization method should be the synchronized keyword to achieve synchronization. The wait / notification mode can be implemented with Object's wait () and notify () series methods. The Condition interface also provides a monitor method similar to Object, which works with Lock to implement the wait / notification mode, but there are differences in usage and functional features between the two. Some comparisons between Object and Condition interfaces. From "the Art of Java concurrent programming"
Introduction and examples of Condition interface
First of all, we need to understand that the condition object is dependent on the lock object, which means that the condition object needs to be created through the lock object (calling the newCondition () method of the Lock object). The way to use consition is very simple. However, you need to be careful to acquire the lock before calling the method.
Package com.ydl.test.juc;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class ConditionUseCase {public Lock lock = new ReentrantLock (); public Condition condition = lock.newCondition (); public static void main (String [] args) {ConditionUseCase useCase = new ConditionUseCase (); ExecutorService executorService = Executors.newFixedThreadPool (2) ExecutorService.execute (new Runnable () {@ Override public void run () {useCase.conditionWait ();}}); executorService.execute (new Runnable () {@ Override public void run () {useCase.conditionSignal ();}}) } public void conditionWait () {lock.lock (); try {System.out.println (Thread.currentThread (). GetName () + "got the lock"); System.out.println (Thread.currentThread (). GetName () + "wait for the signal"); condition.await (); System.out.println (Thread.currentThread (). GetName () + "get the signal") } catch (Exception e) {} finally {lock.unlock ();}} public void conditionSignal () {lock.lock (); try {Thread.sleep (5000); System.out.println (Thread.currentThread (). GetName () + "got the lock"); condition.signal () System.out.println (Thread.currentThread (). GetName () + "signal");} catch (Exception e) {} finally {lock.unlock ();}
1 pool-1-thread-1 got the lock.
2 pool-1-thread-1 waiting for signal
3 pool-1-thread-2 got the lock.
4 pool-1-thread-2 sends out signal
As shown in the example, the Condition object is generally used as a member variable. When the await () method is called, the current thread releases the lock and waits here, while other threads call the signal () method of the Condition object, notifying the current thread that the current thread returns from the await () method and has acquired the lock before returning.
2. Common methods of Condition interface
Condition can be popularly understood as conditional queue. When a thread calls the await method, it is not awakened until a condition that the thread waits for is true. This approach provides a simpler wait / notification mode for threads. Condition must be used with locks because access to shared state variables occurs in a multithreaded environment. An instance of Condition must be bound to a Lock, so Condition is generally implemented as an internal Lock.
Await (): causes the current thread to wait until it is signaled or interrupted.
Await (long time, TimeUnit unit): causes the current thread to wait until it is signaled, interrupted, or reaches a specified wait time.
AwaitNanos (long nanosTimeout): causes the current thread to wait until it is signaled, interrupted, or reaches the specified wait time. The return value indicates the remaining time. If you wake up before nanosTimesout, the return value = nanosTimeout-elapsed time, if the return value is
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.