- Processor가 복수 개의 Threads를 같은 시간대에 처리하는 것이다.
- 한 프로그램 내의 어떤 기능은 병렬적으로 처리해도 되기 때문에 Multi-threads를 사용한다.
- 이미 컴퓨터에서 볼 수 있는 Multi-processing과 비슷하다.
- 한 프로그램 안에 독립적으로 실행할 수 있는 부분을 Thread라고 부른다.
- Thread는 가벼운 Process라고 볼 수 있다.
일상생활에서 많이 이루어지고 있다.
이를 컴퓨터에 적용하는 것.
가장 넓은 범위이다.
- Multicore Processing - Multicore를 사용한다.
- Multi-processing - 여러 프로그램
- Multi-threading - 한 프로그램
- Extending the Thread Class.
- Implementing the Runnable Interface
- Implements java.lang.Runnable.
- Override the run() method.
- Instantiate a Thread object.
- Invoke start() method.
/*
* Program Name: DateDisplayerRunnable.java
* Program Description: DateDisplayerRunnable is multi-threading java program Implementing 'Runnable' interface.
* Name: 심현솔
* Student ID: 20201736
*/
import java.lang.Runnable;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateDisplayerRunnable implements Runnable {
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
for (int i=0; i<7; i++) {
try {
System.out.println(Thread.currentThread().getName() + " : " + sdf.format(new Date()));
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
Thread thread0 = new Thread(new DateDisplayerRunnable());
Thread thread1 = new Thread(new DateDisplayerRunnable());
System.out.println("Testing Program 2 using Runnable Interface");
thread0.start();
thread1.start();
}
}
- Extend java.lang.Thread.
- Override the run() method.
- Instantiate a Thread object.
- Invoke start() method.
/*
* Program Name: DateDisplayerThread.java
* Program Description: DateDisplayerThread is multi-threading java program extending 'Thread' class.
* Name: 심현솔
* Student ID: 20201736
*/
import java.lang.Thread;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateDisplayerThread extends Thread {
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
for (int i=0; i<7; i++) {
try {
System.out.println(this.getName() + " : " + sdf.format(new Date()));
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
System.out.println("Testing Program 1 using Thread Class");
new DateDisplayerThread().start();
new DateDisplayerThread().start();
}
}