
| 구분 | 싱글 쓰레드 (Single Thread) | 멀티 쓰레드 (Multithreading) |
| 개념 | 프로그램 내 작업을 한 번에 하나씩 순차 실행 | 여러 작업을 동시에(또는 빠르게 번갈아가며) 실행 |
| 처리 방식 | 한 줄씩 순서대로 처리 | 여러 쓰레드가 병렬 또는 동시 처리 시도 |
| 장점 | 구조가 단순하고 디버깅 쉬움 | CPU 활용 극대화, 프로그램 응답성 및 속도 향상 |
| 단점 | 느린 처리, 응답 지연 발생 가능 | 설계 복잡, 동기화 문제로 인한 버그 발생 위험 |
| 사용 예 | 단순 계산, 일괄 처리 | GUI, 웹서버, 게임, 실시간 데이터 처리 등 |
java.lang.Thread 클래스로 쓰레드를 관리class MyThread extends Thread {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(getName() + " 실행 중: " + i);
try {
Thread.sleep(300); // 0.3초 쉬기
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class MyThread extends Thread {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(getName() + " 실행 중: " + i);
try {
Thread.sleep(300); // 0.3초 쉬기
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " 실행 중: " + i);
try {
Thread.sleep(300); // 0.3초 쉬기
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " 실행 중: " + i);
try {
Thread.sleep(300); // 0.3초 쉬기
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}public class ThreadDemo {
public static void main(String[] args) {
// Thread 상속 사용
MyThread thread1 = new MyThread();
thread1.setName("Thread-1");
thread1.start(); // run() 대신 start() 호출해야 쓰레드 실행
// Runnable 구현 사용
Thread thread2 = new Thread(new MyRunnable());
thread2.setName("Thread-2");
thread2.start();
}
}public class ThreadDemo {
public static void main(String[] args) {
// Thread 상속 사용
MyThread thread1 = new MyThread();
thread1.setName("Thread-1");
thread1.start(); // run() 대신 start() 호출해야 쓰레드 실행
// Runnable 구현 사용
Thread thread2 = new Thread(new MyRunnable());
thread2.setName("Thread-2");
thread2.start();
}
}| 상태 | 설명 |
| New | 쓰레드 객체 생성 후 start() 호출 전 상태 |
| Runnable | 실행 대기 또는 실행 중 상태 |
| Blocked | 락(lock) 획득 대기 상태 |
| Waiting | 다른 쓰레드 작업 완료 대기 상태 |
| Timed Waiting | 일정 시간 동안 대기 상태 |
| Terminated | run() 메서드 종료로 쓰레드 작업 완료 상태 |
synchronized 키워드로 동기화 영역 지정 → 한 번에 한 쓰레드만 접근 허용class SafeCounter {
private int count = 0;
// 동기화 메서드로 안전하게 증가 처리
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}class SafeCounter {
private int count = 0;
// 동기화 메서드로 안전하게 증가 처리
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}public class ThreadingExample {
// 동기화 처리된 카운터 클래스
static class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
// Runnable 구현, Counter 공유
static class Worker implements Runnable {
private Counter counter;
public Worker(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
counter.increment();
System.out.println(Thread.currentThread().getName() + " 증가시킨 카운트: " + counter.getCount());
try {
Thread.sleep(200); // 잠시 쉬기
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Counter sharedCounter = new Counter();
Thread t1 = new Thread(new Worker(sharedCounter));
Thread t2 = new Thread(new Worker(sharedCounter));
t1.setName("쓰레드-A");
t2.setName("쓰레드-B");
t1.start();
t2.start();
}
}public class ThreadingExample {
// 동기화 처리된 카운터 클래스
static class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}
// Runnable 구현, Counter 공유
static class Worker implements Runnable {
private Counter counter;
public Worker(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
counter.increment();
System.out.println(Thread.currentThread().getName() + " 증가시킨 카운트: " + counter.getCount());
try {
Thread.sleep(200); // 잠시 쉬기
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Counter sharedCounter = new Counter();
Thread t1 = new Thread(new Worker(sharedCounter));
Thread t2 = new Thread(new Worker(sharedCounter));
t1.setName("쓰레드-A");
t2.setName("쓰레드-B");
t1.start();
t2.start();
}
}ExecutorService 같은 쓰레드 풀 관리 API 사용 권장