java中的 java.util.concurrent.locks.ReentrantLock类中的lockInterruptibly()方法介绍

时间:2021-09-11 05:45:32

在java的 java.util.concurrent.locks包中,ReentrantLock类实现了lock接口,lock接口用于加锁和解锁限制,加锁后必须释放锁,其他的线程才能进入到里面执行,否则出现死锁现象。


lockInterruptibly()方法介绍:

此方法返回的为获取锁的方法,但是当线程调用了interrupt()方法后,此方法将会返回一个异常,导致线程的中断。即线程中断。


代码实例如下:

package TestThread.ThreadLockDemo;

import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();// 初始化lock对象
Test2 test2 = new Test2("苹果", 100);// 初始化苹果的数量
Test1 test1 = new Test1(lock, 10, test2);// 初始化线程对象
Thread t1 = new Thread(test1, "线程1");// 创建线程
Thread t2 = new Thread(test1, "线程2");
Thread t3 = new Thread(test1, "线程3");
Thread t4 = new Thread(test1, "线程4");
Thread t5 = new Thread(test1, "线程5");
// t1.start();// 启动线程
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;
} @Override
public void run() {
// try {
// lock.lock();
// Thread.sleep(1);
// System.out.println(Thread.currentThread().getName() + ":开锁成功");
// } catch (InterruptedException e) {
// e.printStackTrace();
// } finally {
// lock.unlock();
// System.out.println(Thread.currentThread().getName() + ":闭锁成功");
// } // lock.tryLock()测试
// if (lock.tryLock()) {
// try {
// // Thread.sleep(1000);
// System.out.println(Thread.currentThread().getName() + ":当前线程已经开启锁!");
// } catch (Exception e) {
// System.out.println(Thread.currentThread().getName() + ":当前线程中断了!");
// } finally {
// lock.unlock();
// System.out.println(Thread.currentThread().getName() + ":释放锁成功!");
// }
// } else {
// System.out.println(Thread.currentThread().getName() + ":未获取到锁!");
// }
try {
lock.lockInterruptibly();
try {
System.out.println(Thread.currentThread().getName() + ":当前线程获取线程锁!");
} catch (Exception e) {
System.out.println(Thread.currentThread().getName() + ":当前线程发生系统异常!");
} finally {
lock.unlock();
System.out.println(Thread.currentThread().getName() + ":释放锁成功!");
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + ":当前线程中断!");
}
}
} class Test2 {
private String name;
int count; /**
* @param name苹果的名字
* @param count初始化苹果的数量
*/
public Test2(String name, int count) {
this.name = name;
this.count = count;
} /**
* * @author 作者 E-mail:
*
* @date 创建时间:2017年3月24日 下午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() + ":苹果的数量为:" + this.count + ",卖掉了" + discount);
}
}

运行结果如下:

java中的 java.util.concurrent.locks.ReentrantLock类中的lockInterruptibly()方法介绍

更新代码如下:

package TestThread.ThreadLockDemo;

import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();// 初始化lock对象
Test2 test2 = new Test2("苹果", 100);// 初始化苹果的数量
Test1 test1 = new Test1(lock, 10, test2);// 初始化线程对象
Thread t1 = new Thread(test1, "线程1");// 创建线程
Thread t2 = new Thread(test1, "线程2");
Thread t3 = new Thread(test1, "线程3");
Thread t4 = new Thread(test1, "线程4");
Thread t5 = new Thread(test1, "线程5");
Thread t6 = new Thread(test1, "线程6");
// t1.start();// 启动线程
t2.start();
t4.start();
t5.start();
t4.interrupt();
t3.start();
t6.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;
} @Override
public void run() {
try {
lock.lockInterruptibly();
try {
Thread.sleep(1000);
test2.DiscountApple(count);
// Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + ":当前线程发生系统异常!");
} finally {
lock.unlock();
System.out.println(Thread.currentThread().getName() + ":释放锁成功!");
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + ":当前线程中断!");
}
}
} class Test2 {
private String name;
int count; /**
* @param name苹果的名字
* @param count初始化苹果的数量
*/
public Test2(String name, int count) {
this.name = name;
this.count = count;
} /**
* * @author 作者 E-mail:
*
* @date 创建时间:2017年3月24日 下午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() + ":苹果的数量为:" + this.count + ",卖掉了" + discount);
}
}

执行结果为:

java中的 java.util.concurrent.locks.ReentrantLock类中的lockInterruptibly()方法介绍