HelloThread - 스레드 생성
스레드를 생성해보자.
클래스에 Thread 를 상속 받고 run 메서드를 재정의하면 된다!
public class HelloThread extends Thread {
@Override
public void run() {
System.out.println(
Thread.currentThread().getName() + " : run()");
}
}
HelloThreadMain
- 해당 코드를 실행하는 스레드 이름을 출력한다 Thread.currentThread().getName()
- 그래서 메인 메서드를 실행하는 main 이라는 스레드명이 제일 처음에 출력된다.
- 누가 이 코드를 실행하는지 스레드명을 확인하면서 학습하자.
public static void main(String[] args) {
/** Thread.currentThread().getName() -> 스레드명이 main 이라고 출력된다 */
System.out.println(Thread.currentThread().getName() + ": main() start");
HelloThread helloThread = new HelloThread(); // HelloThread 객체를 생성
System.out.println(Thread.currentThread().getName() + ": start() 호출 전 ");
helloThread.start(); // start() 를 호출한다 !
// 이 시점부터 main 스레드와 Thread-0 이 동시에 실행된다.
System.out.println(Thread.currentThread().getName() + ": start() 호출 후 ");
System.out.println(Thread.currentThread().getName() + ": main() end");
}
- HelloThread 인스턴스를 생성하고 HelloThread helloThread = new HelloThread()
- 인스턴스 생성만 됬을 뿐, 스택 프레임이 할당된 게 아니다.
- start() 를 호출한다.

helloThread.start() 를 호출하면, HelloThread 의 스택 프레임이 생성된다.
(main 스레드와는 별도의 스택 프레임이다)
그리고 HelloThread 가 run() 를 호출한다.
주의할 점은 main 스레드가 아니라, HelloThread 가 run() 을 호출한다는 점이다.
main 스레드는 단지 helloThread.start() 호출을 통해서 다른 스레드에게 일을 시작하라고 지시할 뿐이다.
- 스레드는 동시에 실행되기 때문에 실행 순서는 얼마든지 달라질 수 있다.
main: main() start
main: start() 호출 전
main: start() 호출 후
Thread-0 : run()
main: main() end
- 실행 할 때마다 조금 달라진다. Thread-0 이 제일 밑에 출력될 때도 있다
정리
- 스레드 객체를 생성하고, 반드시 start() 를 호출해야 스택 공간을 할당 받고 스레드가 작동한다.
- 스레드는 순서와 실행 기간을 모두 보장하지 않는다!
Runnable 인터페이스
- 자바가 제공하는 스레드 실행용 인터페이스다.
public interface Runnable {
void run();
}
어떻게 사용하는지 살펴보자.
HelloRunnable implements Runnable
public class HelloRunnable implements Runnable {
@Override
public void run() { // run() 내에 작업 로직을 구현한다
System.out.println(Thread.currentThread().getName() + ": run()");
}
}
HelloRunnableMain
- 실행 결과는 기존과 같다.
- 차이가 있다면, Thread 와 해당 스레드가 실행할 작업이 분리되어 있다는 점이다.
- Thread 객체를 생성할 때, 생성자로 작업 클래스를 전달한다.
public class HelloRunnableMain {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + ": main() start");
HelloRunnable runnable = new HelloRunnable(); // 작업 정의
Thread thread = new Thread(runnable); // 스레드
thread.start();
System.out.println(Thread.currentThread().getName() + ": main() end");
}
}
Runnable 인터페이스 구현하는 방식이 더 나은 이유!
Thread 클래스 상속 방식의 단점
- 자바는 단일 상속만을 허용하므로, Thread 클래스를 상속받으면, 다른 클래스를 상속받을 수 없다.
- 인터페이스를 사용하는 방식에 비해 유연성이 떨어진다.
Runnable 인터페이스를 구현하는 방식
- 상속의 자유로움
- 코드의 분리: 스레드와 실행할 작업은 분리하여 코드의 가독성을 높일 수 있다.
- 여러 스레드가 동일한 Runnable 객체를 공유할 수 있어 자원 관리를 효율적으로 할 수 있다!
728x90
'프로그래밍 > JAVA' 카테고리의 다른 글
스레드 생성과 실행 (0) | 2025.04.03 |
---|---|
List (0) | 2025.03.28 |
Exception (0) | 2025.03.20 |
String 클래스 (1) | 2025.03.20 |
자바 객체 지향 (0) | 2025.03.13 |