여러 쓰레드 만들기

김건우·2024년 7월 16일

여러 쓰레드를 우선 만들어보고 , 해당 쓰레드를 그림으로 표현해보겠다.

public class MyLoggerMain {
    public static void main(String[] args) {
        log("main() start");

        HelloRunnable runnable = new HelloRunnable();
        Thread thread1 = new Thread(runnable);
        thread1.start();
        Thread thread2 = new Thread(runnable);
        thread2.start();
        Thread thread3 = new Thread(runnable);
        thread3.start();

        log("main() end");
    }
}

  • 스레드3개를 생성할 때 모두 같은 HelloRunnable 인스턴스( x001 )를 스레드의 실행 작업으로 전달했다.
    *Thread-0 , Thread-1 , Thread-2 는 모두 HelloRunnable 인스턴스에 있는 run() 메서드를 실행한다.
profile
Live the moment for the moment.

0개의 댓글