ここでコードを取得し、実行すると、1秒あたりの単語を出力する新しいスレッドを開始します.5秒後にメインメソッドがスレッドを停止します。だからプログラムは5つの言葉を印刷し、それは....しかし私のラップトップでのみ私の家のコンピュータではありません。私の家のコンピュータでは6回プリントします、なぜですか?Javaのスレッドが同時に起動する
public class Main {
public static void main (String [] args){
try {
T1 t1 = new T1();
System.out.println("Creating and staring thread 1\n");
Thread.sleep(5000);
t1.stopThread();
} catch(InterruptedException ie) {}
}
}
public class T1 extends Thread{
private boolean alive = true;
public T1(){
start();
}
public void run(){
while(alive){
try {
System.out.println("Tråd T1: Tråd 1");
Thread.sleep(1000);
} catch(InterruptedException ie) {}
}
}
public void stopThread(){
alive = false;
}
}
よく理解してください。 http://stackoverflow.com/questions/824110/accurate-sleep-for-java-on-windows- http://www.javatuning.com/why-is-thread-sleep-inherently-inaccurate/ – SHiRKiT