์๋ฐ ๋ฉํฐ์ฐ๋ ๋ ๊ตฌํํ๊ธฐ
์๋ฐ์์ ์ฐ๋ ๋๋ฅผ ์ด์ฉํ์ฌ ๋น๋๊ธฐ๋ก ์์ ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ๊ฐ๋ตํ๊ฒ ํฌ์คํ ํฉ๋๋ค.
์๋ฐ์์ ์ฐ๋ ๋๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง๊ฐ ์์ต๋๋ค.
์ฒซ ๋ฒ์งธ๋ Runnable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌ Thread ์์ฑ์๋ก ํด๋น ๊ตฌํ์ฒด๋ฅผ ๋๊ฒจ์ฃผ๋ ๋ฐฉ๋ฒ์ด๊ณ ,
๋ ๋ฒ์งธ๋ ์ง์ Thread ํด๋์ค๋ฅผ ์์ํ๋ ๊ฒ์ ๋๋ค.
์ฐ์ ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ์ผ๋ก ์ฐ๋ ๋๋ฅผ ์์ฑํ์ฌ ์คํํ๋ ์ฝ๋๋ฅผ ๋ณด๊ฒ ์ต๋๋ค.
public static void main(String[] args) {
Runnable task = new Task();
Thread thread = new Thread(task);
thread.start();
}
static class Task implements Runnable {
int num = 0;
@Override
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(num++);
}
}
}
์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ์๋์ ๊ฐ์ด ์ถ๋ ฅ์ด ๋ฉ๋๋ค.
0
1
2
3
4
5
6
7
8
9
์ด๋ฒ์๋ Thread ํด๋์ค๋ฅผ ์์ํ์ฌ ๋์ผํ ์์ (task)์ ํ๋๋ก ํด๋ณด๊ฒ ์ต๋๋ค.
public static void main(String[] args) {
ThreadTask task = new ThreadTask();
task.run();
}
static class ThreadTask extends Thread {
int num = 0;
@Override
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(num++);
}
}
}
์คํํ๋ฉด Runnable๋ก ๊ตฌํํ์ ๋์ ๋์ผํ๊ฒ ์๋์ ๊ฐ์ด ์ถ๋ ฅ์ด ๋ฉ๋๋ค.
0
1
2
3
4
5
6
7
8
9
์ฐธ๊ณ ๋ก ์ค์ Thread ํด๋์ค๋ฅผ ์ด์ด๋ณด๋ฉด Runnable ์ธํฐํ์ด์ค๋ฅผ implementํ๊ณ ์์ต๋๋ค.
public class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
private volatile String name;
private int priority;
private Thread threadQ;
private long eetop;
.
.
.
๊ทธ๋ผ ์ด๋ฒ์๋ ์ฐ๋ ๋ ๋ ๊ฐ๋ฅผ ๋์์ ๋๋ ค๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
public static void main(String[] args) {
Runnable task = new Task();
Thread thread1 = new Thread(task);
thread1.start();
System.out.println("thread 1 executed.");
Thread thread2 = new Thread(task);
thread2.start();
System.out.println("thread 2 executed.");
System.out.println("์ฐ๋ ๋ ์ฝ ์ข
๋ฃ");
}
static class Task implements Runnable {
int num = 0;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ", num=" + num++);
}
}
}
์ค๊ฐ์ค๊ฐ์ ์ด๋ป๊ฒ ์คํ์ด ๋๋์ง ํ์ธ์ ํ๊ธฐ ์ํ์ฌ System.out.println์ผ๋ก ๋ก๊น ์ ํด๋ดค์ต๋๋ค.
thread 1 executed.
Thread-0, num=0
Thread-0, num=1
Thread-0, num=2
Thread-0, num=3
Thread-0, num=4
Thread-0, num=5
thread 2 executed.
์ฐ๋ ๋ ์ฝ ์ข
๋ฃ
Thread-0, num=6
Thread-1, num=7
Thread-1, num=9
Thread-0, num=8
Thread-1, num=10
Thread-0, num=11
Thread-1, num=12
Thread-1, num=13
Thread-1, num=14
Thread-1, num=16
Thread-1, num=17
Thread-1, num=18
Thread-1, num=19
Thread-0, num=15
BUILD SUCCESSFUL in 0s
2 actionable tasks: 1 executed, 1 up-to-date
์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์ฐ๋ ๋๊ฐ ๋จผ์ ์์๋์ง๋ง "task1 executed"๊ฐ ๋จผ์ ์ถ๋ ฅ์ด ๋์๊ณ ๊ทธ ๋ค์ ์ฒซ ๋ฒ์งธ ์ฐ๋ ๋์ธ Thread-0์ด ์ถ๋ ฅ์ ์์ํ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ค๊ฐ์ "task2 executed"๊ฐ ์ถ๋ ฅ๋ ๊ฑธ ๋ณด๋ ๋ ๋ฒ์งธ ์ฐ๋ ๋๊ฐ ์คํ์ด ๋ ๊ฒ์ ์ ์ ์์ฃ . ๋ ๋ฒ์งธ ์ฐ๋ ๋๋ Thread-1 ์ด๋ฉฐ ์ฒซ ๋ฒ์งธ ์ฐ๋ ๋๊ฐ ์ผ์ ๋ง์น๊ธฐ ์ ์ ์์๋์ด "Thread-1, num=0"์ ์ถ๋ ฅํ ๊ฒ์ ํ์ธ ํ ์ ์์ต๋๋ค.
์ด๋ ๊ฒ ํ๋์ ํ๋ก์ธ์ค(main ํ๋ก์ธ์ค) ์์์ ์ฌ๋ฌ ๊ฐ์ ์์ ์ ๋์์ ๋๋์ด ์คํ ํ ์ ์๋๋ก ํด์ฃผ๋ ๊ฒ์ด ๋ฐ๋ก ์ด ์ฐ๋ ๋์ ๋๋ค.
์ฐ๋ ๋๋ก ์์ ์ ํ๊ฒ ๋๋ฉด ์๋์ ๊ฐ์ ํน์ง๋ค์ด ์์ต๋๋ค.
- ์ฌ๋ฌ ์์ ์ ๋์์ ์ฒ๋ฆฌํ ์ ์์ด ์์ ์ ์๋ฃํ๋๋ฐ ํ์ํ ์ด ์์ ์๊ฐ์ด ์ค์ด๋ ๋ค. (๋ฉ์ธ์ฐ๋ ๋ 1๊ฐ๋ก ์์ ํ์ ๋์ ๋น๊ตํ์ ๋)
- ๋จผ์ ์์ํ ์ฐ๋ ๋๊ฐ ํญ์ ๋จผ์ ์ผ์ ๋๋ด์ง๋ ์๋๋ค. ๋ฐ๋ผ์, ์์ ์ ์์๊ฐ ์ค์ํ ๋์๋ ์ฐ๋ ๋๋ก ๋๋์ด ์ฒ๋ฆฌํ๋ฉด ์๋๋ค.
์, ์ด์ ์ฑ๊ธ ์ฐ๋ ๋๋ก 10์๊ฐ ๊ฑธ๋ฆด ์ผ์ 100๊ฐ์ ์ฐ๋ ๋๋ฅผ ๋๋ ค์ 10๋ถ๋ง์ ๋๋ด๊ณ ์ถ์ต๋๋ค. ์ด๋ป๊ฒ ํด์ผ ํ ๊น์?
์ฐ๋ ๋๋ฅผ 100๊ฐ๋ฅผ ๋ง๋ค์ด์ผ ํ๋๋ฐ ์๋์ฒ๋ผ ๋ฌด์ํ๊ฒ ๋ง๋ค์ด์ผ ํ ๊น์?
Thread thread1 = new Thread(task);
Thread thread2 = new Thread(task);
Thread thread3 = new Thread(task);
Thread thread4 = new Thread(task);
Thread thread5 = new Thread(task);
.
.
.
์๋๋๋ค. ์ด๋ ๊ฒ ํด์ผ๋๋ฉด ์ฐ๋ ๋ ๋ง๋ค๋ค๊ฐ ํด๊ทผํด์ผ๋ฉ๋๋ค.
์๋ฐ์์๋ java.util.concurrentํจํค์ง์ ๋์์์ ์ ์ํด ์ ์ฉํ ํด๋์ค๋ค์ ๋ชจ์๋จ๋๋ฐ ๊ทธ์ค์ ThreadPoolExecutor๋ผ๋ ๋ ์์ด ์์ต๋๋ค. ์ด ๋ ์์ ์ฌ์ฉํ๋ฉด ๋ ์ธ ์ค์ด๋ฉด 100๊ฐ์ ์ฐ๋ ๋๋ฅผ ๋๋ฆด ์ ์๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ฃ .
ThreadPoolExecutor์ ๋ํด ์ค๋ช ํ๋ ค๋ฉด ํฌ์คํธ๊ฐ ๊ธธ์ด์ง๋ ThreadPoolExecutor๋ฅผ ์ด์ฉํ ๋ฉํฐ์ฐ๋ ๋ ๊ตฌํ์ ๊ณ์ ์ด์ด๊ฐ๊ฒ ์ต๋๋ค.