线程休眠目是使线程让出CPU最简单做法的当休眠定时间后线程会苏醒进入准备状态等待执行
线程休眠思路方法是Thread.sleep(long millis) 和Thread.sleep(long millis, nanos) 均为静态思路方法那sleep休眠哪个线程呢?简单说哪个线程sleep就休眠哪个线程
/**
* Java线程:线程调度-休眠
*
* @author leizhimin 2009-11-4 9:02:40
*/
public Test {
public void (String args) {
Thread t1 = MyThread1;
Thread t2 = Thread( MyRunnable);
t1.start;
t2.start;
}
}
MyThread1 extends Thread {
public void run {
for ( i = 0; i < 3; i) {
.out.prln("线程1第" + i + "次执行!");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.prStackTrace;
}
}
}
}
MyRunnable implements Runnable {
public void run {
for ( i = 0; i < 3; i) {
.out.prln("线程2第" + i + "次执行!");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.prStackTrace;
}
}
}
}
线程2第0次执行!
线程1第0次执行!
线程1第1次执行!
线程2第1次执行!
线程1第2次执行!
线程2第2次执行!
Process finished with exit code 0
从上面结果输出可以看出无法精准保证线程执行次序
本文出自 “熔 岩” 博客 http://lavasoft.blog.51cto.com/62575/221790
最新评论