Description of the lockInterruptibly () method in the java. util. concurrent. locks. locks. ReentrantLock class in java, concurrent. locks

Source: Internet
Author: User

Description of the lockInterruptibly () method in the java. util. concurrent. locks. locks. ReentrantLock class in java, concurrent. locks

In java. util. concurrent. in the locks package, the ReentrantLock class implements the lock interface. The lock interface is used to lock and unlock restrictions. After the lock is applied, the lock must be released and other threads can enter the lock for execution. Otherwise, a deadlock may occur.

 

LockInterruptibly () method:

This method returns the method for obtaining the lock, but when the thread calls the interrupt () method, this method returns an exception, resulting in thread interruption. Thread interruption.

 

The code example is as follows:

 

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"); Thread t4 = new Thread (test1, "Thread 4"); Thread t5 = new Thread (test1, "Thread 5 "); // t1.start (); // start thread t2.start (); t4.start (); t5.start (); t4.interrupt (); t3.start ();}} class Test1 implements Runnable {private int count; private ReentrantLock lock; private Test2 test2; public Test1 (ReentrantLock lock, int count, Test2 test2) {this. lock = lock; this. count = count; this. test2 = test2;} @ Overridepublic void Run () {// try {// lock. lock (); // Thread. sleep (1); // System. out. println (Thread. currentThread (). getName () + ": Unlock successful"); //} catch (InterruptedException e) {// e. printStackTrace (); //} finally {// lock. unlock (); // System. out. println (Thread. currentThread (). getName () + ": Locked successfully"); //} // lock. tryLock () test // if (lock. tryLock () {// try {/// Thread. sleep (1000); // System. out. println (Thread. currentThread (). ge TName () + ": the current thread has enabled the lock! "); //} Catch (Exception e) {// System. out. println (Thread. currentThread (). getName () +": the current Thread is interrupted! "); //} Finally {// lock. unlock (); // System. out. println (Thread. currentThread (). getName () +": the lock is released successfully! "); //} Else {// System. out. println (Thread. currentThread (). getName () +": No lock is obtained! "); //} Try {lock. lockInterruptibly (); try {System. out. println (Thread. currentThread (). getName () +": the current Thread obtains the Thread lock! ");} Catch (Exception e) {System. out. println (Thread. currentThread (). getName () +": the current Thread has a System Exception! ") ;}Finally {lock. unlock (); System. out. println (Thread. currentThread (). getName () +": the lock is released successfully! ") ;}} Catch (InterruptedException e) {System. out. println (Thread. currentThread (). getName () +": the current Thread is interrupted! ") ;}} 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 );}}
The running result is as follows:

 

 

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.