일반적으로 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){
}
}
}
위의 실행결과는 어떻게 될까
start로 runnable 단계로 올립니다. a객체인 경우 한번 더 올라가기 때문에 exception이 발생할 수 밖에 없습니다.
위의 코드에서
a.run(), a.run(), b.run(), b.run() 으로 변경하면 어떨까요?
실제로 생성은 되지만 스레드의 메터정보가 runnable 상태에 포함되지 않습니다.
결과로는
current thread: main
thread: main
thread: main
thread: main
thread: main
위와 같은 결과 나오게 됩니다.
결론으로
start()메서드는 해당스레드가 run이 가능한 상태로 만들어주는 메서드로 생각하시면 됩니다