• 코드:
​x
 
1
class ThreadWithClass extends Thread {
2
    public void run() {
3
        for (int i = 0; i < 5; i++) {
4
            System.out.println(getName());  // 현재 실행 중인 스레드의 이름을 반환함.
5
            try {
6
                Thread.sleep(10);           // 0.01초간 스레드를 멈춤.
7
            } catch (InterruptedException e) {
8
                e.printStackTrace();
9
            }
10
        }
11
    }
12
}
13
​
14
class ThreadWithRunnable implements Runnable {
15
    public void run() {
16
        for (int i = 0; i < 5; i++) {
17
            System.out.println(Thread.currentThread().getName());   // 현재 실행 중인 스레드의 이름을 반환함.
18
            try {
19
                Thread.sleep(10);
20
            } catch (InterruptedException e) {
21
                e.printStackTrace();
22
            }
23
        }
24
    }
25
}
26
​
27
public class prog {
28
    public static void main(String[] args){
29
        ThreadWithClass thread0 = new ThreadWithClass();        // Thread 클래스를 상속받는 방법
30
        Thread thread1 = new Thread(new ThreadWithRunnable());  // Runnable 인터페이스를 구현하는 방법
31
        
32
        thread0.start();    // 스레드의 실행
33
        thread1.start();    // 스레드의 실행
34
    }
35
}
표준입력 & 실행옵션