[Java] Thread

양정훈·2022년 6월 16일
0

Thread

  • 우선 순위에 의해 다수의 쓰레드들이 실행된다
  • 우선순위는 Thread 클래스의 상수인 MIN_PRIORITY ~ MAX_PRIORITY 사이의 숫자가 배정된다
  • 더 높은 우선순위의 Thread가 실행되면 현재 실행중인 Thread는 강제 중단된다
class Foo extends Thread {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}

Thread t = new Foo();
t.start();
t.stop();

Runnable

  • 이미 다른 클래스를 상속해서, Thread 상속으로 구현이 불가할때 사용한다
class Foo implements Runnable {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}

Foo f = new Foo();
Thread t = new Thread(f);

// 축약
Thread t = new Thread(new Foo());

t.start();

Join

Thread t = new Foo();
t.start();

t.join();
System.out.println("Hello World!"); // t 가 끝날때까지 출력되지 않는다

Interrupt

synchronized, wait, notify

0개의 댓글