Java. util. concurrent. locks. ReentrantLock class usage, concurrent. locks

Source: Internet
Author: User

Java. util. concurrent. locks. ReentrantLock class usage, concurrent. locks
The lock class is implemented as follows: Explanation of the ReentrantLock interface method:

The lock () method is used to obtain the lock object. If the lock is not obtained, the lock is always obtained.

Trylock (): A boolean value. It indicates whether the lock is obtained. If the lock is not obtained, false is returned. If the lock is obtained, true is returned.

TryLock (long timeout, TimeUnit unit): indicates that the lock is obtained within the specified time. If the lock is not obtained, false is returned; otherwise, true is returned.

Unlock (): To release the lock, other threads have the opportunity to execute it.

LockInterruptibly (): gets the lock. If the thread is interrupted, an exception is thrown.

Example code:

Package TestThread. threadLockDemo; import java. util. concurrent. locks. reentrantLock; public class TestLock {public static void main (String [] args) {ReentrantLock lock = new ReentrantLock (); // initialize the lock Object Test2 test2 = new Test2 ("apple ", 100); // initialize the number of Apple Test1 test1 = new Test1 (lock, 10, test2); // initialize the Thread object Thread t1 = new Thread (test1, "Thread 1"); // create Thread t2 = new Thread (test1, "Thread 2"); Thread t3 = new Thread (te St1, "thread 3"); t1.start (); // start thread t2.start (); t3.start () ;}} class Test1 implements Runnable {private int count; private ReentrantLock; private Test2 test2; public Test1 (ReentrantLock lock, int count, Test2 test2) {this. lock = lock; this. count = count; this. test2 = test2 ;}@ Overridepublic void run () {lock. lock (); // yoke try {test2.DiscountApple (count);} catch (Exception e) {System. out. print ("an exception occurred when calling the method for selling Apple! ");} Finally {lock. unlock (); // unlock }}class Test2 {private String name; int count; /*** @ param name: Apple name * @ param count Number of Apple initiated */public Test2 (String name, int count) {this. name = name; this. count = count;}/***** @ author E-mail: ** @ date Creation Time: march 24, 2017 1:13:14 * @ version 1.0 * @ parameter * @ since * @ return */public void DiscountApple (int discount) {this. count = this. count-discount; System. out. println (Thread. currentThread (). getName () + ": Number of apples:" + this. count + ", sold" + discount );}}

Instance result:

Trylock () method usage Demonstration:

 

Package TestThread. threadLockDemo; import java. util. concurrent. locks. reentrantLock; public class TestLock {public static void main (String [] args) {ReentrantLock lock = new ReentrantLock (); // initialize the lock Object Test2 test2 = new Test2 ("apple ", 100); // initialize the number of Apple Test1 test1 = new Test1 (lock, 10, test2); // initialize the Thread object Thread t1 = new Thread (test1, "Thread 1"); // create Thread t2 = new Thread (test1, "Thread 2"); Thread t3 = new Thread (te St1, "thread 3"); t1.start (); // start thread t2.start (); t3.start () ;}} class Test1 implements Runnable {private int count; private ReentrantLock; private Test2 test2; public Test1 (ReentrantLock lock, int count, Test2 test2) {this. lock = lock; this. count = count; this. test2 = test2 ;}@ Overridepublic void run () {if (lock. tryLock () {// lock. lock (); // yoke try {test2.DiscountApple (count);} catch (Exception e) {System. out. Print ("an exception occurred when calling the method for selling Apple! ");} Finally {lock. unlock (); // unlock} else {System. out. println (Thread. currentThread (). getName () + "no lock obtained") ;}} class Test2 {private String name; int count; /*** @ param name: Apple name * @ param count Number of Apple initiated */public Test2 (String name, int count) {this. name = name; this. count = count;}/***** @ author E-mail: ** @ date Creation Time: march 24, 2017 1:13:14 * @ version 1.0 * @ parameter * @ since * @ return */public void DiscountApple (int discount) {this. count = this. count-discount; System. out. println (Thread. currentThread (). getName () + ": Number of apples:" + this. count + ", sold" + discount );}}

Test results:

 

 

unlock()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.