`

Re entrantLock usage

阅读更多

 a thread can be really interrupted by aThread.interrupt() only under 1 situation -- in blocked status wrapped with catch(InterruptedException e) {...}. Precisely,

 

 a) aThread.sleep();

 b) aObject.wait();

 c) BlockingQueue operations;

 d) Lock.lockInterruptibly();

 

Regarding d). When a thread is waiting outside of some lock, previously, you can't interrupt it, the thread will just block there if he can not get the lock --- dead lock.

 

In Java 5, Lock.lockInterruptibly() needs to be wrapped with InterruptedException, which means you can save the thread from stupid waiting.

 

=====Code example ======

 

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

{

lock.lock();

//lock.lockInterruptibly(); ---- c)

System.out.println(Thread.currentThread().getName() + " running");

TimeUnit.SECONDS.sleep(20); ----- b)

lock.unlock();

System.out.println(Thread.currentThread().getName() + " finished");

}

catch (InterruptedException e)

{

System.out.println(Thread.currentThread().getName() + " interrupted");

}

 

}

}

 

---Output---

Thread-0 running  ----  a)

Thread-0 finished

Thread-1 running

Thread-1 interrupted

 

Explain:

a) Actually at here, thread1 has already been interrupted. But thread1 has to wait 20 seconds later, till thread0 has finished running, and thread1 goes to place b), at that place, thread1 can goto catch block! That’s quite late!

 

 

If you change from lock.lock() to lock.lockInterruptibly()

 

---Output---

Thread-0 running

Thread-1 interrupted

Thread-0 finished

 

Because c) has been monitored with InterruptedException, when thread0 is working, thread1 is waiting outside of lock, if you interrupt thread1, thread1 will immediately goes to catch block.

 

 

分享到:
评论

相关推荐

    USB HID Usage Tables 1.3

    《USB HID Usage Tables 1.3》是USB Implementers’ Forum发布的一份关于USB设备中人机交互设备(Human Interface Device, HID)使用表的最新版本,涵盖了从1996年到2022年的更新。这份文档是USB设备开发、设计和...

    USB HID USAGE TABLE V1.12--2004.10.28.rar

    USB HID USAGE TABLE V1.12 是一个关于通用串行总线(USB)人机交互设备(Human Interface Device,简称HID)使用的标准文档。这个版本发布于2004年10月28日,提供了对USB HID设备中使用的各种输入、输出和特征报告...

    USB HID USAGE TABLE v1.21最新版

    USB HID Usage Tables v1.21 是一个重要的文档,对于进行USB设备开发,特别是涉及到USB Human Interface Device (HID) 部分的开发者来说,它是不可或缺的参考资源。USB HID是USB设备类别之一,主要用于连接键盘、...

    CSS减肥工具Firefox插件CSS Usage

    标题中的“CSS减肥工具Firefox插件CSS Usage”指的是一个针对Firefox浏览器的扩展,该扩展专门设计用于优化和精简CSS(层叠样式表)代码。CSS是网页设计中用于控制布局和样式的语言,但随着时间的推移,CSS文件可能...

    USB HID USAGE TABLE.pdf

    首先,关于USB HID Usage Tables的版本和修订历史,我们可以看到文档提到了多个版本和修订号,例如版本1.12(rc1修订版,发布于2004年10月28日)和版本1.11(发布于2001年6月27日)。修订号后的日期表示了文档的更新...

    Firefox_css_usage-0.2.9.zip

    《Firefox CSS Usage插件详解与应用》 在Web开发领域,优化网页性能是至关重要的,而CSS(Cascading Style Sheets)作为控制网页样式的重要工具,其精简和优化直接影响到页面加载速度和用户体验。Firefox的CSS ...

    HID Usage Tables V1.12

    根据给定文件的信息,我们可以详细地探讨一下USB HID(Human Interface Device)Usage Tables v1.12的相关知识点。 ### HID Usage Tables v1.12概述 HID Usage Tables v1.12是一份关于USB Human Interface Device ...

    Android 使用记录访问权限(PACKAGE-USAGE-STATS)、UsageStatsManager

    Android 使用记录访问权限(PACKAGE_USAGE_STATS)、UsageStatsManagerAndroid 使用记录访问权限(PACKAGE_USAGE_STATS)、UsageStatsManagerAndroid 使用记录访问权限(PACKAGE_USAGE_STATS)、...

    Chart-CPUUsage

    标题"Chart-CPUUsage"和描述都指明了我们的主要任务是利用Chart控件来展示计算机CPU的使用情况。 首先,让我们了解`Chart`控件在.NET Framework中的作用。`Chart`控件是ASP.NET Web Forms的一个强大工具,它允许...

    Firefox插件 CSS Usage

    css usage是一个基于firebug的firefox扩展,可以用来查看页面中的CSS的使用情况,可以清楚的查看css文件中所有的规则在你的网站中的 真实的使用情况。可以查看一个网站中多个页面中的css使用情况——这个很好,可以...

    HID usage table

    标题中的“HID usage table”指的是人机交互设备(Human Interface Device)的使用表,这是USB设备类定义中的一部分,特别是在设计和实现USB接口的人体输入设备时非常重要。HID设备包括键盘、鼠标、游戏控制器、触摸...

    USB HID usage table

    USB HID usage tableUSB键值,可以下来看看

    css Usage.zip

    第一步,我们需要安装Firefox,或者确定你...第四步,在浏览器中打开我们要优化的页面(本地的页面也可以),点击右下角的firebug小图标,打开firebug工具窗口,我们会看到在工具选项中我们有一个 CSS Usage工具的按钮。

    usb_Usage_Tables_for_HID_PowerDevices_HID_Usage_Tables_hid_pdf_U

    标题中的“usb_Usage_Tables_for_HID_PowerDevices_HID_Usage_Tables_hid_pdf_U”指的是USB(通用串行总线)设备中HID(Human Interface Device,人机交互设备)使用的表格,特别是关于HID电源设备的使用规范。...

    802.11ac usage model

    802.11ac usage model,ppt,英文

    CpuUsage(显示cpu利用动态曲线图)

    《CpuUsage:掌握CPU利用率动态曲线图》 在计算机性能监控中,了解CPU的利用率是至关重要的。CpuUsage工具就是为此目的而设计的,它能够实时地展示CPU利用率的动态曲线图,类似于Windows的任务管理器。这篇文章将...

    Asset Usage Finder v4.13

    Usage search for ALL types of assets, e.g.: Scripts, Shaders, Scenes, Materials, Sprites, Prefabs, Fonts, Textures, Sounds... Search for usages in: Prefabs, Scenes, Atlases, Materials and others ...

    Asset Usage Finder 4.13.unitypackage

    可以轻松为您找到所有资源的 用法/依赖关系/参考的资产 在项目,场景和嵌套式预制舞台中

Global site tag (gtag.js) - Google Analytics