📋 스레드
- 한 프로세스 내에서 두 가지 이상의 일을 동시에 처리할 수 있음
✅ Thread
package section17.ex01;
class Sample extends Thread {
// Sample() {
// setName("MyTask-1"); // 스레드 이름 수정1
// }
@Override
public void run() {
// System.out.println("thread run.");
System.out.println(Thread.currentThread().getName()); // 현재 스레드명
}
}
public class Jump1701 {
public static void main(String[] args) {
// System.out.println(Thread.currentThread().getName()); // 현재 스레드명
Sample sample = new Sample();
sample.setName("MyTask-1"); // 스레드 이름 수정2
sample.start();
}
}
✅ Join
package section17.ex02;
import java.util.ArrayList;
class Sample extends Thread {
int seq;
public Sample(int seq) {
this.seq = seq;
}
@Override
public void run() {
System.out.println(this.seq + " thread start.");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(this.seq + " thread end.");
}
}
public class Jump1702 {
public static void main(String[] args) {
Thread t = new Sample(1);
t.start();
try {
t.join(); // 스레드가 종료될때 까지 대기
} catch (Exception e) {
e.printStackTrace();
}
// 10개 스레드의 경우
// ArrayList<Thread> threads = new ArrayList<>();
// for (int i = 0; i < 10; i++) {
// Thread t = new Sample(i);
// t.start();
// threads.add(t);
// }
// for (int i = 0; i < threads.size(); i++) {
// Thread t = threads.get(i);
// try {
// t.join();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
System.out.println("main end.");
}
}
✅ Runnable 인터페이스
- Thread 클래스를 상속하면 Thread 클래스를 상속한 클래스가 다른 클래스를 상속할 수 없기 때문에 Runnable 인터페이스를 구현함
package section17.ex03;
import java.util.ArrayList;
class Sample implements Runnable {
int seq;
public Sample(int seq) {
this.seq = seq;
}
@Override
public void run() {
System.out.println(this.seq + " thread start.");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(this.seq + " thread end.");
}
}
public class Jump1703 {
public static void main(String[] args) {
ArrayList<Thread> threads = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Thread t = new Thread(new Sample(i)); // 수정
t.start();
threads.add(t);
}
for (int i = 0; i < threads.size(); i++) {
Thread t = threads.get(i);
try {
t.join();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("main end.");
}
}
✅ 스레드 동기화
- 멀티 스레드 프로그램에서 단 하나의 스레드만 처리할 수 있는 영역을 ”임계 영역”이라고 함.
- 하나의 스레드가 이 영역에 진입할 때 락을 걸어 다른 스레드가 수행되지 못하도록 하고 작업이 종료되면 락을 풀어서 다른 스레드가 작업하도록 함
package section17.ex04;
class Counter {
private int count;
public int getCount() {
return this.count;
}
public void setCount() {
this.count++;
}
// 1. 메서드 동기화
// public synchronized void setCount() {
// this.count++;
// }
}
class Sample extends Thread {
private Counter counter;
int seq;
public Sample(int seq, Counter counter) {
this.seq = seq;
this.counter = counter;
}
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
counter.setCount();
System.out.println(this.seq + "thread add count " + counter.getCount());
}
} catch (Exception e) {
e.printStackTrace();
}
// 2. 블록 동기화: 성능 고려 전체 메서드가 아닌 특정 영역만 동기화 처리
// synchronized (counter) {
// try {
// for (int i = 0; i < 10; i++) {
// Thread.sleep(1000);
// counter.setCount();
// System.out.println(this.seq + "thread add count " + counter.getCount());
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
}
public class Ex1701 {
public static void main(String[] args) {
Counter counter = new Counter();
Sample sample1 = new Sample(1, counter);
Sample sample2 = new Sample(2, counter);
sample1.start();
sample2.start();
}
}
✳️ printStackTrace()