1. 스레드 시작 1

“자바 메모리 구조”

image.png

“스레드 생성”

A. 스레드 생성 - Thread 상속

public class HelloThread extends Thread{

    @Override
    public void run() {

        System.out.println(Thread.currentThread().getName() + ": run()");
    }
}
public class HelloThreadMain {

    public static void main(String[] args) {

        System.out.println(Thread.currentThread().getName()+ ": main() start");

        HelloThread helloThread = new HelloThread();
        System.out.println(Thread.currentThread().getName()+ ": start() 호출 전");
        helloThread.start(); // start 호출 시 HelloThread가 run() 메서드를 실행 -> HelloThread에 맡기고 main에서는 다른 일을 한다!
        // start() 메서드를 호출해야 run() 코드가 실행된다.
        System.out.println(Thread.currentThread().getName()+ ": start() 호출 후");

        System.out.println(Thread.currentThread().getName()+ ": main() end");
    }
}

실행 결과

main: main() start
main: start() 호출 전
main: start() 호출 후
main: main() end
Thread-0: run()

스레드 생성 전