线程的优先级以及休眠问题
媛猿 2018-07-15 12:13:10 在下是一个在校学生,学艺不精,求学问道。
代码如下:
package test.wang;
public class Thead_Test implements Runnable{
private int num;
public int getNum() {
return num;
}
private Thread thread;
public Thead_Test(){
num = 99;
new Thead_Test2(this);
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while(true){
num--;
if(num < 1){
System.out.println("test over!");
}else{
System.out.println(num);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Thead_Test2 implements Runnable{
private Thead_Test test;
private Thread thread;
public Thead_Test2(Thead_Test thead_Test){
this.test = thead_Test;
thread = new Thread(this);
thread.start();
}
public void run() {
while(true){
int num = test.getNum();
System.out.println(" " + num);
try {
thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我在另一个类里将Thead_Test实例化,然后结果输出会出现以下情况:
99
98
97
98
96
96
95
95
94
94
93
93
92
93