方法Thread.yield
:
現在実行中のスレッドオブジェクトを一時的に一時停止し、他のスレッドの実行を許可します。
したがって、次のコードでは:
public class Test implements Runnable {
private int stopValue;
public Fib(int stopValue){
this.stopValue = stopValue;
}
@Override
public void run() {
System.out.println("In test thread");
for(int i = 0; i < stopValue; i++){
c = i + 1;
}
System.out.println("Result = "+c);
}
public static void main(String[] args){
int defaultStop = 1024;
if(args.length > 0){
defaultStop = Integer.parseInt(args[0]);
}
Thread a = new Thread(new Fib(defaultStop));
System.out.println("In main");
a.setDaemon(true);
a.start();
Thread.yield();
System.out.println("Back in main");
}
}
私は私が見るべきだと期待しています:
In main
それからIn test thread
残りは未定義になります。しかし、なぜ時々私が見るだけなのか理解できません:
In main
そしてスレッドBack in main
からの印刷ステートメントはありませんか?Test