1.프로세스
- 스레드
- 스레드의 생성과 실행
package main;
public class MainClass {
public static void main(String[] args) {
ThreadWithClass thread1 = new ThreadWithClass(); //쓰레드 클래스를 상속받는 방법
Thread thread2 = new Thread(new ThreadWithRunnable()); //Runnable 인터페이스를 구현하는 방법
thread1.start();
thread2.start();
}
}
class ThreadWithClass extends Thread{
public void run() {
for(int i=0; i<5; i++) {
System.out.println(getName()+" : 1"); //현재 실행 중인 스레드의 이름을 반환.
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(Exception e) {
e.printStackTrace();
}
}
}
}