如何做到当某线程拿不到锁时不继续等待立刻退出?
2013-03-13 09:22
274 查看
一、代码
1、Worker类
2、测试启动类
3、测试结果
testSynchronized()执行结果:
A start
A work
A over
C start
C work
B start
B work
B over
C over
D start
D work
D over
testTryLock()执行结果:
B start
B work
B over
D start
D over
A start
A over
C start
C over
testLockInterruptibly()执行结果:
A start
D start
B start
C start
A work
A over
二、说明
1、使用synchronized加锁时,拿不到锁的线程会一直等待直到得到锁才继续执行,并不会退出。
2、使用lock.tryLock()的方式只能保证拿不到锁的线程不会执行tryLock(){}包括起来的代码,不在这个范围内的后续代码当拿到锁以后还会执行。
3、使用lock.lockInterruptibly()时,拿不到锁的线程执行到这一句就会判断当前线程是否拥有lock,一旦发现lock已经锁住就结束线程,不会执行后续代码。
补充:上面第3点不可取,虽然拿不到锁的线程不会继续执行下去,但这些线程会保持休眠状态(参考http://hi.baidu.com/gefforey520/item/202295fcf256e35fc8f3370a):
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
The lock is acquired by the current thread; or
Some other thread the current thread.
。
1、Worker类
package com.leslie.test; import java.util.concurrent.locks.ReentrantLock; public class Worker { private Object o = new Object(); private ReentrantLock lock = new ReentrantLock(); public void work1(String action) { System.out.println(action + " start"); synchronized (o) { System.out.println(action + " work"); } System.out.println(action + " over"); } public void work2(String action) { System.out.println(action + " start"); if (lock.tryLock()) { System.out.println(action + " work"); } System.out.println(action + " over"); } public void work3(String action) { System.out.println(action + " start"); try { lock.lockInterruptibly(); lock.lock(); System.out.println(action + " work"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } System.out.println(action + " over"); } }
2、测试启动类
package com.leslie.test; public class Tester { private static final Worker worker = new Worker(); public static void testSynchronized(){ Thread t1 = new Thread(){ public void run() {worker.work1("A");} }; Thread t2 = new Thread(){ public void run() {worker.work1("B");} }; Thread t3 = new Thread(){ public void run() {worker.work1("C");} }; Thread t4 = new Thread(){ public void run() {worker.work1("D");} }; t1.start(); t2.start(); t3.start(); t4.start(); } public static void testTryLock(){ Thread t1 = new Thread(){ public void run() {worker.work2("A");} }; Thread t2 = new Thread(){ public void run() {worker.work2("B");} }; Thread t3 = new Thread(){ public void run() {worker.work2("C");} }; Thread t4 = new Thread(){ public void run() {worker.work2("D");} }; t1.start(); t2.start(); t3.start(); t4.start(); } public static void testLockInterruptibly(){ Thread t1 = new Thread(){ public void run() {worker.work3("A");} }; Thread t2 = new Thread(){ public void run() {worker.work3("B");} }; Thread t3 = new Thread(){ public void run() {worker.work3("C");} }; Thread t4 = new Thread(){ public void run() {worker.work3("D");} }; t1.start(); t2.start(); t3.start(); t4.start(); } public static void main(String[] args) { testSynchronized(); } }
3、测试结果
testSynchronized()执行结果:
A start
A work
A over
C start
C work
B start
B work
B over
C over
D start
D work
D over
testTryLock()执行结果:
B start
B work
B over
D start
D over
A start
A over
C start
C over
testLockInterruptibly()执行结果:
A start
D start
B start
C start
A work
A over
二、说明
1、使用synchronized加锁时,拿不到锁的线程会一直等待直到得到锁才继续执行,并不会退出。
2、使用lock.tryLock()的方式只能保证拿不到锁的线程不会执行tryLock(){}包括起来的代码,不在这个范围内的后续代码当拿到锁以后还会执行。
3、使用lock.lockInterruptibly()时,拿不到锁的线程执行到这一句就会判断当前线程是否拥有lock,一旦发现lock已经锁住就结束线程,不会执行后续代码。
补充:上面第3点不可取,虽然拿不到锁的线程不会继续执行下去,但这些线程会保持休眠状态(参考http://hi.baidu.com/gefforey520/item/202295fcf256e35fc8f3370a):
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
The lock is acquired by the current thread; or
Some other thread the current thread.
。
相关文章推荐
- java并发编程学习:如何等待多个线程执行完成后再继续后续处理(synchronized、join、FutureTask、CyclicBarrier)
- c#如何实现一个线程暂停,等待用户输入文本后继续运行?
- java并发编程学习:如何等待多个线程执行完成后再继续后续处理(synchronized、join、FutureTask、CyclicBarrier)
- 如何实现拨通电话,IVR提示,转人工按1,转传真按2,繁忙时自动转到队列,并播放音乐,并提示按1继续等待,按2退出。
- 如何等待一个线程完成后才继续执行之后的操作
- c#如何实现一个线程暂停,等待用户输入文本后继续运行?
- 线程等待一定时间和及时返回继续执行,以及等待线程退出。
- c#如何实现一个线程暂停,等待用户输入文本后继续运行?
- WinForm程序如何等待多个线程执行完后提示用户?
- Windows系统下等待线程退出的方法
- 线程如何获取另外主动退出的线程的传出指针
- java线程池主线程等待子线程执行完成后再继续处理后面工作
- 如何在退出一个activity后,很好的取消AsyncTask继续运行?
- Java并发:等待事件发生后所有线程继续执行
- Qt线程QThread简析(8个线程等级,在UI线程里可调用thread->wait()等待线程结束,exit()可直接退出线程,setStackSize设置线程堆栈,首次见到Qt::HANDLE,QThreadData和QThreadPrivate)
- 线程状态切换 退出-暂停-继续
- python threading父进程不死,子线程不退出..如何才能使用完线程后回收线程?
- 多线程中主线程等待所有子线程执行完再继续执行的解决方法
- 举例说明如何实现使用线程来实现UC浏览器的等待页面的效果
- 02-03线程的等待(挂起)与唤醒(继续)