프로세스는 여러 스레드를 생성해서 한번에 여러 일을 할 수 있다.
스레드는 프로세스 내에서 생성되며 프로세스의 리소스를 공유한다.

java 에서는 Thread 클래스를 통해 스레드를 생성할 수 있다.
Thread는 Runnable 인터페이스를 구현했는데 Runnable은 run 메서드만을 가진 함수형 인터페이스 이다.
스레드가 할 일을 run 메서드에 정의하고 start메서드를 통해 쉽게 실행할 수 있다.
class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread를 상속한 방식!");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // run() 실행됨
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable을 구현한 방식!");
}
}
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start(); // run() 실행됨
}
}
public class Main {
public static void main(String[] args) {
Thread t0 = new Thread() {
@Override
public void run() {
System.out.println("익명항수를 통한 스레드 생성.");
}
};
t0.start();
}
}| Java Throwable hierarchy (0) | 2025.04.24 |
|---|---|
| Java try catch (0) | 2025.04.24 |
| Java Interface (0) | 2025.04.23 |