下面的lockInterruptibly()就稍微难理解一些。
先说说线程的打扰机制,每个线程都有一个 打扰 标志。这里分两种情况,
1. 线程在sleep或wait,join, 此时如果别的进程调用此进程的 interrupt()方法,此线程会被唤醒并被要求处理InterruptedException;(thread在做IO操作时也可能有类似行为,见java thread api)
2. 此线程在运行中, 则不会收到提醒。但是 此线程的 “打扰标志”会被设置, 可以通过isInterrupted()查看并 作出处理。
lockInterruptibly()和上面的第一种情况是一样的, 线程在请求lock并被阻塞时,如果被interrupt,则“此线程会被唤醒并被要求处理InterruptedException”。
我写了几个test,验证一下:
1). lock()忽视interrupt(), 拿不到锁就 一直阻塞:
@Test
public void test3() throws Exception{
final Lock lock=new ReentrantLock();
lock.lock();
Thread.sleep(1000);
Thread t1=new Thread(new Runnable(){
@Override
public void run() {
lock.lock();
System.out.println(Thread.currentThread().getName()+” interrupted.”);
}
});
t1.start();
Thread.sleep(1000);
t1.interrupt();
Thread.sleep(1000000);
}
可以进一步在Eclipse debug模式下观察,子线程一直阻塞。
2). lockInterruptibly()会响应打扰 并catch到InterruptedException
@Test
public void test4() throws Exception{
final Lock lock=new ReentrantLock();
lock.lock();
Thread.sleep(1000);
Thread t1=new Thread(new Runnable(){
@Override
public void run() {
try {
lock.lockInterruptibly();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+” interrupted.”);
}
}
});
t1.start();
Thread.sleep(1000);
t1.interrupt();
Thread.sleep(1000000);
}
3). 以下实验验证:当线程已经被打扰了(isInterrupted()返回true)。则线程使用lock.lockInterruptibly(),直接会被要求处理InterruptedException。
@Test
public void test5() throws Exception{
final Lock lock=new ReentrantLock();
Thread t1=new Thread(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(2000);
lock.lockInterruptibly();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+” interrupted.”);
}
}
});
t1.start();
t1.interrupt();
Thread.sleep(10000000);
}
[来源:http://www.dewen.org/q/9077]
分类目录
- arch/management (17)
- computer (38)
- java/j2ee (304)
- lnmpa (237)
- mac/iphone/ipad/android (11)
- mysql/oracle/postgresql (126)
- os/software (74)
- other (518)
- python (6)
- redis/memcached/mongo (31)
- sitebuild (143)
随便看看
标签云
程序员 创业 人生箴言 eclipse 快捷键 术语 索引 unix命令 vim wordpress java学习笔记 环境变量 oracle内置函数 index 人生 数据类型 nohup tuxedo mysql学习笔记 MS-DOS命令 servlet spring 职场进阶 职业进阶 服务器选购 服务器选型 apache JPA MongoDB 注解 tomcat 子女教育 jquery maven JVM aix命令 网络营销 java异常 seo 人生规划 关键字 css 网络推广 struts 系统优化 成长 frame iframe bluehost jdbc select 我的信仰 oracle函数 cookie HashMap 站长工具 乱码 ArrayList secureCRT jsp session tail find halt 事务 oracle单记录函数 算法 URL window table javascript操作表单元素 String 字符串处理 健康 http 域名 情感 more google A记录 域名解析 netstat 弹出对话框 弹出窗口 框架集 框架 excel 字符串 javascript函数 showModalDialog nginx number 数组 sql frameset 开源程序 java数组 软件 oracle服务友情链接
收藏链接