class ThreadWithClass extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(getName()); // 현재 실행 중인 스레드의 이름을 반환함.
try {
Thread.sleep(10); // 0.01초간 스레드를 멈춤.
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ThreadWithRunnable implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()); // 현재 실행 중인 스레드의 이름을 반환함.
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class prog {
public static void main(String[] args){
ThreadWithClass thread0 = new ThreadWithClass(); // Thread 클래스를 상속받는 방법
Thread thread1 = new Thread(new ThreadWithRunnable()); // Runnable 인터페이스를 구현하는 방법
thread0.start(); // 스레드의 실행
thread1.start(); // 스레드의 실행
}
}