1.2.3休眠(Sleeping)
来源:互联网 发布:知妈妈乐疯狂 编辑:程序博客网 时间:2024/05/18 00:43
Thread类声明一对static方法让线程处于休眠状态:
(1)void sleep(long millis):休眠millis毫秒。线程休眠的实际毫秒数取决于系统定时器和调度程序的精度和准确性。当millis不合法,将会抛出IllegaArgumentException异常;当任何线程的当前线程被打断,将会抛出IllegaArgumentException异常。当这个异常抛出之后,就可以说明当前线程是处于打断状态。
(2)void sleep(long millis, intnanos):休眠从millis毫秒到nanos纳秒。线程休眠的实际毫秒数取决于系统定时器和调度程序的精度和准确性。当millis不合法,nanos不合法,或nanos大于999999,将会抛出IllegaArgumentException异常;当任何线程的当前线程被打断,将会抛出IllegaArgumentException异常。当这个异常抛出之后,就可以说明当前线程是处于打断状态。
sleep()方法理适合用于繁忙的循环,因为它们不会消耗处理器周期。下面是给出的例子。Listing 1-4.
Listing1-4 证明线程休眠。
package com.owen.thread.chapter1;public class ThreadSleeping{public static void main(String[] args){Runnable r = new Runnable(){@Overridepublic void run(){String name = Thread.currentThread().getName();int count = 0;while (!Thread.interrupted())System.out.println(name + ": " + count++);}};Thread thdA = new Thread(r);Thread thdB = new Thread(r);thdA.start();thdB.start();try{Thread.sleep(2000);} catch (InterruptedException ie){}thdA.interrupt();thdB.interrupt();}}源码下载:git@github.com:owenwilliam/Thread.git
阅读全文
0 0
- 1.2.3休眠(Sleeping)
- Windows操作系统---电脑睡眠状态与休眠状态的区别(The Difference of Sleeping Mode and Hibernatiing Mode)
- Unity 理解刚体(Rigidbody)和碰撞体(Collider)和触发器(Is Trigger)以及刚体休眠(Rigidbody Sleeping)
- Unity 理解刚体(Rigidbody)和碰撞体(Collider)和触发器(Is Trigger)以及刚体休眠(Rigidbody Sleeping)
- Sleeping
- Sleeping
- hdu3905 Sleeping (区间dp)
- Linux device driver ---sleeping(睡眠唤醒)
- Sleeping会话导致阻塞原理(下)
- Sleeping会话导致阻塞原理(下)
- Sleeping会话导致阻塞原理(下)
- Sleeping会话导致阻塞原理(下)
- Sleeping会话导致阻塞原理(下)
- Sleeping会话导致阻塞原理(下)
- eclipse总是在Building WorkSpace(sleeping)
- Go Go Sleeping!Sleeping!
- 关于render (转自http://www.cnblogs.com/sleeping/)
- 操作系统实验,IPC(一):sleeping barber,睡眠理发师问题
- Python之数据可视化神奇-Matplotlib
- 【LectCode】15. 3Sum
- 直接插入排序-java实现
- 怎么用弱引用实现内存泄漏检测
- bash: yum install crontabs: command not found
- 1.2.3休眠(Sleeping)
- C++黑框闪退解决措施
- HDU 2438 Turn the corner 三角函数+三分查找法
- leetcode解题报告19. Remove Nth Node From End of List
- 2017 山东理工第九届校赛 J LL
- Text Generation With LSTM Recurrent Neural Networks in Python with Keras
- 1.3总结
- JS原生数据类型检测
- Opencv学习——图像阈值化