Thread란 무엇인가요?
스레드란?
프로그래밍에서 여러 작업을 동시에 실행하기 위한 작업의 단위
스레드를 사용하면 다른 작업들을 동시에 실행할 수 있어서 프로그램의 성능을 향상시킬 수 있습니다. 스레드(Thread) 간단한 예시 입니다. 동시에 3가지의 출력을 진행하였고, 3가지 출력이 동시에 진행되기 때문에 순서가 번갈아가며 출력되는 것을 볼 수 있습니다.
MainThread
package thread;
public class MainThread {
public static void main(String[] args) throws InterruptedException {
// 쓰레드를 만드는 방법
// 1.Thread 클래스를 상속받아서 만드는 방법
// 2.Runnable 인터페이스의 구현체를 만들어서 사용하는 방법
Thread01 t01 = new Thread01();
Runnable r02 = new Thread02();
Thread t02 = new Thread(r02);
t01.start();
t02.start();
int i=0;
while(i<=10) {
System.out.println("메인 : " + i);
Thread.sleep(100);
i++;
}
}
}
Thread01
package thread;
public class Thread01 extends Thread{
@Override
public void run() {
int i=1;
try {
while(i<=10) {
System.out.println("스레드extends :"+i);
Thread.sleep(100);
i++;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Thread02
package thread;
public class Thread02 implements Runnable{
@Override
public void run() {
int i=1;
try {
while(i<=10) {
System.out.println("스레드implements :"+i);
Thread.sleep(100);
i++;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
출력 결과
'IT관련 > 이론' 카테고리의 다른 글
AVL Tree - AVL 트리란? (0) | 2024.06.12 |
---|---|
DTO, DAO, VO 차이 - JAVA (0) | 2024.05.30 |
인터페이스란? java interface 예시 (0) | 2024.05.28 |
SQL 스토어드 프로시저 만드는 방법 (0) | 2024.05.13 |
MariaDB SQL - DDL, DML, DCL (0) | 2024.05.03 |