public class ThreadMain {
public static void process() {
for(int i=1; int<=50; i++) {
System.out.println(i);
}
}
public static void main(String[] args) {
process();
process();
}
}
// -------------- Thread 클래스 상속 -----------------
public class ThreadEx extends Thread {
@Override
public void run() { // 스레드 클래스에서 상속받은 후 재정의 시키는 것!
for(int i=1; int<=50; i++) {
System.out.println(i);
}
}
}
// -------------- Runnable 인터페이스 상속 --------------
public class ThreadEx2 implements Runnable {
@Override
public void run() { // 스레드 클래스에서 상속받은 후 재정의 시키는 것!
for(int i=1; int<=50; i++) {
System.out.println(i);
}
}
}
// 스레드 실행 main 문
public class ThreadMain {
public static void main(String[] args) {
Thread t1 = new ThreadEx();
Runnable r = new ThreadEx2(); ==> 클래스를 따로 만들지 않고 익명 구현 객체 생성(or 람다)로도 구현 가능하다.
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
1
2
3
4
1
2
...
1~50이 끝까지 출력되지 않은 것으로 t1, t2는 독립적으로 실행되는 것임을 확인할 수 있다.