参考
http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/util/concurrent/locks/ReentrantLock.html#lockInterruptibly();
http://tomboxfan.javaeye.com/blog/368270;
lockInterruptibly 与 lock比较区别在于
lockInterruptibly 优先考虑响应中断,而不是响应锁定的普通获取或重入获取
如果a处 是lock.lock();
输出
Thread-0 running
(这里休眠了20s)
Thread-0 finished
Thread-1 running
Thread-1 interrupted
============================
如果a处是lock.lockInterruptibly()
Thread-0 running
Thread-1 interrupted
(这里休眠了20s)
Thread-0 finished
=========================
如果a处是lock.tryLock();
Thread-1 running
Thread-0 running
(这里休眠了20s)
Thread-1 finished
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at test.RunIt3.run(TTTT.java:32)
at java.lang.Thread.run(Unknown Source)
=========================
如果a处是lock.tryLock(10,TimeUnit.SECONDS);
Thread-0 running
(这里休眠了10s)
Thread-1 running
(这里休眠了10s)
Thread-0 finished
(这里休眠了10s)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at test.RunIt3.run(TTTT.java:32)
at java.lang.Thread.run(Unknown Source)
http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/util/concurrent/locks/ReentrantLock.html#lockInterruptibly();
http://tomboxfan.javaeye.com/blog/368270;
lockInterruptibly 与 lock比较区别在于
lockInterruptibly 优先考虑响应中断,而不是响应锁定的普通获取或重入获取
- package test;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- public class TTTT {
- public static void main(String[] args){
- Thread i1 = new Thread(new RunIt3());
- Thread i2 = new Thread(new RunIt3());
- i1.start();
- i2.start();
- i2.interrupt();
- }
- }
- class RunIt3 implements Runnable{
- private static Lock lock = new ReentrantLock();
- public void run(){
- try{
- //---------------------------------a
- lock.lock();
- //lock.lockInterruptibly();
- System.out.println(Thread.currentThread().getName() + " running");
- TimeUnit.SECONDS.sleep(20);
- lock.unlock();
- System.out.println(Thread.currentThread().getName() + " finished");
- }
- catch (InterruptedException e){
- System.out.println(Thread.currentThread().getName() + " interrupted");
- }
- }
- }
如果a处 是lock.lock();
输出
Thread-0 running
(这里休眠了20s)
Thread-0 finished
Thread-1 running
Thread-1 interrupted
============================
如果a处是lock.lockInterruptibly()
Thread-0 running
Thread-1 interrupted
(这里休眠了20s)
Thread-0 finished
=========================
如果a处是lock.tryLock();
Thread-1 running
Thread-0 running
(这里休眠了20s)
Thread-1 finished
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at test.RunIt3.run(TTTT.java:32)
at java.lang.Thread.run(Unknown Source)
=========================
如果a处是lock.tryLock(10,TimeUnit.SECONDS);
Thread-0 running
(这里休眠了10s)
Thread-1 running
(这里休眠了10s)
Thread-0 finished
(这里休眠了10s)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at test.RunIt3.run(TTTT.java:32)
at java.lang.Thread.run(Unknown Source)
没有评论:
发表评论