线程常用方法isInterrupted() VS interrupted()和Sleep() VS Suspend()
来源:互联网 发布:js position fixed 编辑:程序博客网 时间:2024/06/04 19:26
isInterrupted() VS interrupted()
1、源码解析
1)interrupted() 测试当前线程是否被中断,是返回true,否返回false
public static boolean interrupted() { return currentThread().isInterrupted(true); }
2)isInterrupted()测试该线程是否被中断,是返回true,否返回false
public boolean isInterrupted() { return isInterrupted(false); }
private native boolean isInterrupted(boolean ClearInterrupted);2、区别
这两种方法都可以在Thread类中判断线程是否通过interrupt方法终止
区别是:
1)interrupted是静态的方法,可以直接通过类名调用;isInterrupted是非静态的,需要通过实例对象来调用。
2)interrupted是用来判断当前线程是否被中断。isInterrupted可以用来判断其他线程是否被中断
Sleep() VS Suspend()
1、源码解析
1)sleep()使当前运行的线程暂时的停止,从运行状态到阻塞,millis之后,进入runnable状态,等待JVM的线程调度。
public static native void sleep(long millis) throws InterruptedException;
注意:
1、sleep方法有两个重载形式,其中一种不仅仅可以设毫秒还可以设纳秒,但大多数OS平台上的JAVA虚拟机无法精确到毫秒,因此JVM取最接近这个值的毫秒
2、使用sleep方法时必须使用throws或try{...}catch{...}。因为run方法无法使用throws
@Deprecated public final void suspend() { checkAccess(); suspend0(); }
参考资料:http://developer.51cto.com/art/200911/162925.htm
0 0
- 线程常用方法isInterrupted() VS interrupted()和Sleep() VS Suspend()
- java isInterrupted 和interrupted方法区别
- java isInterrupted()和interrupted()
- interrupted和isInterrupted
- interrupt,interrupted和isInterrupted
- 线程中断,interrupt(),interrupted,isInterrupted()
- Java中的线程中断:interrupt()、interrupted()和isInterrupted
- 【多线程】interrupt、interrupted、isInterrupted方法
- this.interrupted()和this.isInterrupted()方法的区别
- interrupted()和isInterrupted()的区别
- interrupted() 和 isInterrupted() 的区别
- java中interrupted()和isInterrupted
- interrupt(),interrupted() 和isInterrupted()使用
- 中断线程中 interrupt() isInterrupted interrupted
- 区分线程中的 interrupt() interrupted() isInterrupted()
- 07_线程停止(interrupt,interrupted,isInterrupted)
- interrupt(), isInterrupted(), interrupted() 方法的区别
- interrupted()方法和线程停止
- 外推--竞价 基本知识
- 析构函数允许重载吗?
- (2.1.6)java之String、StringBuilder、StringBuffer
- FOJ 2185 树的路径覆盖 (图)
- Spring的核心机制:依赖注入
- 线程常用方法isInterrupted() VS interrupted()和Sleep() VS Suspend()
- 【ITOO】WCF宿主的几种方式
- git 使用
- Factorials and Trailing Zeroes
- leetcode: Maximum Gap
- LSB最低有效位和MSB最高有效位
- 【安卓开发】用PageTransformer打造更好的动画效果
- 独白0
- 为什么说win的分区是由fat表组织的,而linux的分区是按照块的方式组织的?