class MyThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName());
}
}
}
public class Test {
public static void main(String[] args) {
MyThread my = new MyThread();
Thread t = new Thread(my,"thread-a");
t.start();
for(int i=0;i<100;i++){
System.out.println(i);
if(i>50){
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
代码如上, 我知道在i>50 会强制t运行, 但是t.start()时 线程t 不就已经启动了吗?为什么在i<50时不会输出thread-a
?
黄舟2017-04-17 17:59:58
표시되는 것은 단일 실행의 결과일 뿐입니다(스레드 스케줄링으로 인해 thread-a는 i > 50까지 출력되지 않을 수 있음). 몇 번 더 실행하면 원하는 결과를 볼 수 있습니다. 🎜>