看jstack输出的时候,可以发现很多状态都是TIMED_WAITING(parking),如下所示:

"http-bio-8080-exec-16" #70 daemon prio=5 os_prio=0 tid=0x00007f6088027800 nid=0x3a1f waiting on condition [0x00007f60fcd03000]

java.lang.Thread.State: TIMED_WAITING (parking)

at sun.misc.Unsafe.park(Native Method)

- parking to wait for <0x00000006cb8d7500> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)

at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)

at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)

at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)

at org.apache.tomcat.util.threads.TaskQueue.poll(TaskQueue.java:86)

at org.apache.tomcat.util.threads.TaskQueue.poll(TaskQueue.java:32)

at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:745)

关于LockSupport,查看相关资料,可知LockSupport类是Java6(JSR166-JUC)引入的一个类,提供了基本的线程同步原语。LockSupport实际上是调用了Unsafe类里的函数,归结到Unsafe里,只有两个函数:

public native void unpark(Thread jthread);

public native void park(boolean isAbsolute, long time);

isAbsolute参数是指明时间是绝对的,还是相对的。

仅仅两个简单的接口,就为上层提供了强大的同步原语。

先来解析下两个函数是做什么的。

unpark函数为线程提供“许可(permit)”,线程调用park函数则等待“许可”。这个有点像信号量,但是这个“许可”是不能叠加的,“许可”是一次性的。

比如线程B连续调用了三次unpark函数,当线程A调用park函数就使用掉这个“许可”,如果线程A再次调用park,则进入等待状态。

注意,unpark函数可以先于park调用。比如线程B调用unpark函数,给线程A发了一个“许可”,那么当线程A调用park时,它发现已经有“许可”了,那么它会马上再继续运行。

在JDK 5里面,是用wait/notify/notifyAll来同步的,它没有LockSupport那样的容忍性,所以JDK7 JUC之后几乎都是采用park与unpark实现。 至于其提供的额外监视器参数,主要是jstack排查方便。

我们知道,线程的shutdown从标准的角度来说,就是给线程发送一个interupt,线程自行决定是否响应,具体是否相应的标准如下:

interrupt

public void interrupt()

Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. (这是正确而且必须的行为,否则就有可能会导致共享变量处于不一致的状态)

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.

Throws:

所以,对于那些无法响应中断的线程中的逻辑,我们需要根据isInterupted来判断决定是否终止自己,不过不可否认的是,现实中有很多的应用并没有这么做。关于对中断的处理方式,可参考Java theory and practice: Dealing with InterruptedException 。

最后看一下,对于那些使用park阻塞的线程,是否支持Interrupt,看javadoc是支持的,如下:

关于java中断,讲得比较好的帖子是:

http://agapple.iteye.com/blog/970055

关于LockSupport,可参见:

http://blog.csdn.net/hengyunabc/article/details/28126139

以及java doc参考https://docs.oracle.com/javase/7/docs/api/.

java中的locksupport_java中线程的停止以及LockSupport工具类相关推荐

  1. Java中 LocalDate、LocalTime、LocalDateTime三个时间工具类的使用介绍

    Java中 LocalDate.LocalTime.LocalDateTime三个时间工具类的使用介绍 一.背景: 之前在做项目的过程中,对日期时间类没有一个系统的了解,总是在用的时候去搜索一下,解决 ...

  2. Java中集合相关案例(泛型通配符、Collections工具类、TreeSet、TreeMap、HashMap、HashSet和集合嵌套案例)

    集合 一.集合相关案例 1.泛型通配符案例 2.集合工具类(Collections工具类) 3.TreeSet和TreeMap案例 4.HashMap案例 5.HashSet案例 6.TreeSet案 ...

  3. 自己写的将数组中的“null“字符串转成null的小工具类,报java.lang.NullPointerException

    起初的工具类代码: //转字符"null"为空public static void toNull(Object[] obj){if (obj!=null) {for (int i ...

  4. java(五)-迭代器,数据结构,List,Set ,TreeSet集合,Collections工具类

    day05[迭代器,数据结构,List,Set ,TreeSet集合,Collections工具类] 主要内容 Collection集合的遍历方式: 迭代器. foreach(增强for循环) JDK ...

  5. java内部类、接口、集合框架、泛型、工具类、实现类

    一.内部类 1.成员内部类. (1)成员内部类的实例化: 外部类名.内部类名   变量名=外部类对象.new 内部类名(); class Person{class Test{}}Person p=ne ...

  6. Java并发指南9:AQS共享模式与并发工具类的实现

    一行一行源码分析清楚 AbstractQueuedSynchronizer (三) 转自:https://javadoop.com/post/AbstractQueuedSynchronizer-3 ...

  7. java druid jdbc例子_JDBC【使用Druid连接数据库,DBUtils工具类的使用】

    Druid连接数据库,DBUtils工具类的使用 1.在maven中添加Druid依赖 com.alibaba druid 1.2.0 2.封装Druid连接方法 import com.alibaba ...

  8. java读写excel文件poi_Java利用POI读写Excel文件工具类

    本文实例为大家分享了Java读写Excel文件工具类的具体代码,供大家参考,具体内容如下 package com.test.app.utils; import java.io.File; import ...

  9. JAVA使用POI对Word docx模板文件替换数据工具类

    word模板文件参考下面: Map<String, Object> params = new HashMap<String, Object>(); params.put(&qu ...

最新文章

  1. 如何使用Leangoo自动生成燃尽图
  2. 安装SQL2005 29506错误码的解决方案
  3. 韦老师的开发板和嵌入式书籍赠送
  4. 用ajax传值input file,获取 input type=file 标签的内容,并使用ajax进行请求到服务器...
  5. 如何系统的自学python-如何系统地自学 Python?
  6. MAC下安装与配置MySQL [转]
  7. 数据库—事务—并发控制技术
  8. 神经网络 知识图谱,神经网络基础知识
  9. AJAX,Axio异步框架(对原生AJAX封装)。web分区
  10. 怎样自制微信gif动态表情包?
  11. 数据库中的主键、超键、候选键、外键
  12. VISA 通信command总结
  13. php 自动生成考卷下载,试卷生成器下载-试卷生成器电脑版下载[试题生成]-华军软件园...
  14. WEB学习第四天(网页模型
  15. C语言打印菱形和空心菱形
  16. 美国 android手机号码,格式编辑文本为美国电话号码1(xxx)-xxxx你输入android?
  17. Ar大屏幕互动,面向非专业领域的体验
  18. Powerdesigner将数据表的Name变中文,字段全部变大写
  19. 爬虫----爬虫基本原理
  20. 从入门到深入!java游戏口袋精灵

热门文章

  1. 华为鸿蒙系统封闭,谷歌正式“除名”华为!“亲儿子”荣耀表示:暂不考虑,鸿蒙OS处境尴尬...
  2. antd option宽度自适应_WordPress文章中添加自适应宽度的表格——墨涩网
  3. c语言编常见算法,5个常见C语言算法
  4. 王道操作系统考研笔记——2.3.3 进程互斥的硬件实现方法
  5. Oracle中Number(p,s)的意义
  6. C# GDI+ 实现图片分隔
  7. 前端学习(3285):立即执行函数四
  8. [css] 为什么说css的选择器一般不要超过三级?
  9. 前端学习(2479):接口文档使用
  10. 前端学习(1731):前端系列javascript之发布窗口布局下