threadでrunとstartの違い
start vs run
通常、threadを継承または実装するとrun()メソッドが実装されます.
ではrunを使うのですか?public class RunThreads {
public static void main(String[] args) {
RunThreads runThreads = new RunThreads();
runThreads.runBasic();
}
public void runBasic(){
Worker a = new Worker();
Worker b = new Worker();
System.out.println("current thread:"+Thread.currentThread().getName());
a.start(); a.start(); b.start(); b.start()
try{
a.join(); b.join();
} catch(InterruptedException e){
}
}
}
上の実行結果は何ですか.
runnable startにアップグレードします.オブジェクトの場合は、もう一度上に移動するため、例外が発生します.
上のコードでは
a.run()、a.run()、b.run()、b.run()に変更したらどうですか.
実際には作成されますが、ねじの公製情報はrunnable状態に含まれません.
結果は.
current thread: main
thread: main
thread: main
thread: main
thread: main
結果は次のとおりです.
要するに.
start()メソッドは,各スレッドを実行できるメソッドと見なすことができる.
Reference
この問題について(threadでrunとstartの違い), 我々は、より多くの情報をここで見つけました
https://velog.io/@yonghyun123/thread에서-run과-start의-차이
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
public class RunThreads {
public static void main(String[] args) {
RunThreads runThreads = new RunThreads();
runThreads.runBasic();
}
public void runBasic(){
Worker a = new Worker();
Worker b = new Worker();
System.out.println("current thread:"+Thread.currentThread().getName());
a.start(); a.start(); b.start(); b.start()
try{
a.join(); b.join();
} catch(InterruptedException e){
}
}
}
Reference
この問題について(threadでrunとstartの違い), 我々は、より多くの情報をここで見つけました https://velog.io/@yonghyun123/thread에서-run과-start의-차이テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol