728x90
๐ ์ค๋ ๋๋ฅผ ํต์ ํ๋ ๋ฉ์๋๋ค
์ด๋ฒ ๋ด์ฉ์ ์๊ธฐ ์ํด์๋ "์ค๋ ๋์ ์ํ ๋ณํ"์ ๋ํด์ ์๊ณ ์์ด์ผ ๊น์ด ์๊ฒ ์ฝ๋๋ฅผ ์ดํดํ ์ ์์ ๊ฒ์ด๋ค. ์ค๋ ๋๋ ํ๋ก์ธ์๋ฅผ ํจ๊ป ์ฌ์ฉํ๊ณ ํญ์ ํ๋๋ง ์คํํ๋ค. ๋ ํ ํ๋ก์ธ์ค์ ์๋ ์ค๋ ๋๋ ์์ฐจ์ ์ผ๋ก ์คํํ๊ณ , ํด๋น ์ค๋ ๋์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ ์ง์คํฐ์ ์คํ์ด ์๋ค.
์ค๋ ๋๋ ํ๋ก์ธ์ค์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ ๊ฐ์ด ์ค๋ ๋์ ์ํ ๋ณํ๊ฐ ์๋ค. ์ค๋ ๋์ ์ฅ์ ์ ์ค๋ ๋ ํ ๊ฐ๊ฐ ๋๊ธฐ ์ํ๋ก ๋ณํ ๋ ์ ์ฒด ํ๋ก์ธ์ค๋ฅผ ๋๊ธฐ ์ํ๋ก ๋ฐ๊พธ์ง ์๋๋ค๋ ๊ฒ์ด๋ค. ์คํ ์ํ์ ์ค๋ ๋๊ฐ ๋๊ธฐ ์ํ๊ฐ ๋๋ฉด ๋ค๋ฅธ ์ค๋ ๋๋ฅผ ์คํํ ์ ์๋ค. ๊ทธ๋ฌ๋ ํ๋ก์ธ์ค์ ๋ฌ๋ฆฌ ์๋ก ๋ ๋ฆฝ์ ์ด์ง๋ ์๋ค.(ํ๋ก์ธ์ค์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณต์ ํ๊ธฐ ๋๋ฌธ์)
์ข ๋ ๊น๊ฒ ์๊ณ ์ถ๋ค๋ฉด ๊ฐ์ธ์ ์ผ๋ก ์ด์์ฒด์ ์์ "ํ๋ก์ธ์ค&์ค๋ ๋" ํํธ๋ฅผ ๊ณต๋ถํด๋ณด๋ ๊ฒ์ ์ถ์ฒํ๋ค.
Thread.State | getState() : Returns the state of this thread. |
void | join() : Waits for this thread to die. |
void | join(long millis) : Waits at most millis milliseconds for this thread to die. |
void | join(long millis, int nanos) : Waits at most millis milliseconds plus nanos nanoseconds for this thread to die. |
void | interrupt() : Interrupts this thread. |
์์ ํ์ ์๋ ๋ฉ์๋๋ค์ ์ค๋ ๋๋ฅผ ํต์ ํ๋ ๋ฉ์๋๋ค์ด๋ค. ์ฌ๊ธฐ์ Thread.State๋ฅผ ์ดํด๋ณด๋ฉด "Enum Thread.State" ์ฆ Enum ํด๋์ค์ด๋ค. ์์ธ ๊ฐ์ ์ดํด๋ณด๋ฉด ์๋์ ๊ฐ์ด ์์๋ค์ ๋ชฉ๋ก์ด ์ ์ธ๋์ด ์๋ค. ์์ ์ฒจ๋ถ๋ ์ค๋ ๋ ์ํ ๋ณํ ์ด๋ฏธ์ง์ ๋์กฐํ๋ฉด์ ๋ณด๋ฉด ์ข์ ๊ฒ์ด๋ค.
์ํ | ์๋ฏธ |
NEW | ์์ง ์์๋์ง ์์ ์ค๋ ๋ ์ํ. |
RUNNABLE | Java ๊ฐ์ ๋จธ์ ์์ ์คํ ์ค์ธ ์ค๋ ๋ ์ํ. |
BLOCKED | ๋ชจ๋ํฐ lock์ด ํ๋ฆฌ๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฌ๋ฉด์ ์ค๋ ๋ ์คํ ์ค์ง ์ํ |
WAITING | ์ค๋ ๋๊ฐ ๋๊ธฐ์ค์ธ ์ํ |
TIMED WAITING | ํน์ ์๊ฐ๋งํผ ์ค๋ ๋๊ฐ ๋๊ธฐ์ค์ธ ์ํ |
TERMINATED | ์ค๋ ๋๊ฐ ์ข ๋ฃ๋ ์ํ |
์์ ๋ฅผ ํตํด ์ดํด๋ณด์.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.study;
class SleepThread1 extends Thread {
long sleepTime;
public SleepThread1(long sleepTime) {
this.sleepTime = sleepTime;
}
@Override
public void run() {
try {
System.out.println("Sleeping " + getName());
Thread.sleep(sleepTime);
System.out.println("Stopping " + getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// entry point
public class RunThreads {
public static void main(String[] args) {
RunThreads sample = new RunThreads();
sample.checkThreadState();
}
public void checkThreadState() {
SleepThread1 thread = new SleepThread1(2000);
try {
System.out.println("thread state : " + thread.getState());
thread.start();
System.out.println("thread state(after start) : " + thread.getState());
Thread.sleep(1000);
System.out.println("thread state(after 1 sec) : " + thread.getState());
thread.join();
thread.interrupt();
System.out.println("thread state(after join) : " + thread.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
|
cs |
์ค๋ ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ๋ง ํ์ ๋๋ NEW์ด๊ณ ,
start() ๋ฉ์๋๋ก ์คํ์ํค๋ RUNNABLE๋ก ๋ฐ๋์๋ค.
๋ญ ์์ธํ ๋ณด๋ฉด ๋น์ฐํ ๊ฒฐ๊ณผ์ผ ๊ฒ์ด๋ค.
์ค์ ๋ก ๋๋ ค๋ด๋ ์ ํ ๋ฌธ์ ๊ฐ ์์ ๊ฒ์ด๋ค.
interrupt() ๋ฉ์๋๋ ํ์ฌ ์ํ ์ค์ธ ์ค๋ ๋๋ฅผ ์ค๋จ์ํจ๋ค. ํ์ง๋ง join() ๋ฉ์๋ ํ ์ค๋ ๋๋ ์ด๋ฏธ ์ข ๋ฃ๊ฐ ๋์๊ธฐ ๋๋ฌธ์ ์๋ฌด๋ฐ ์ํฅ์ ์ฃผ์ง ์๊ณ ๋์ด๊ฐ๋ค.
์๋์ ์ด๋ฏธ์ง์ฒ๋ผ interrupt() ๋ฉ์๋๋ฅผ ์๋ก ์ฌ๋ ค๋ณด๊ณ ์คํ์์ผ๋ณด์.
์์ ๊ฐ์ด interrupt ๋์๋ค๊ณ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
์ข ๋ ์์ธ๋ฅผ ๊ฐ์ง๊ณ ์์๋ณด์.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.study;
class SleepThread1 extends Thread {
long sleepTime;
public SleepThread1(long sleepTime) {
this.sleepTime = sleepTime;
}
@Override
public void run() {
try {
System.out.println("Sleeping " + getName());
Thread.sleep(sleepTime);
System.out.println("Stopping " + getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// entry point
public class RunThreads {
public static void main(String[] args) {
RunThreads sample = new RunThreads();
sample.checkJoin();
}
public void checkJoin() {
SleepThread1 thread = new SleepThread1(2000);
try {
thread.start();
thread.join(500);
thread.interrupt();
System.out.println("thread state(after join) : " + thread.getState());
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
์ฌ๊ธฐ์ SleepThread1 ํด๋์ค ์์ฑ ์ 2000 ์ฆ 2์ด๋ฅผ ์์ฑ์๋ก ๋์ ธ์ฃผ๋ฉฐ ์์ํ๊ฒ ๋๋ค. ๊ทธ๋ ๊ฒ start()๋ฅผ ํตํด ์ค๋ ๋๊ฐ ์์ฑ๋๋ฉด run() ๋ฉ์๋๊ฐ ์คํ๋๊ณ , 2์ด ๋๊ธฐ ์ค์ธ ์ํ๋ก ๋น ์ง๋ค. ์ด๋ ์ด์ด๋ฅผ ๊ธฐ๋ค๋ฆฌ์ง๋ง join(500)์ผ๋ก 0.5์ด๋ง ๊ธฐ๋ค๋ฆฌ๋ค๊ฐ ๋ค์ ์ฝ๋๋ก ์งํ์ด ๋๋ค. 0.5์ด ์ง๋ ๋ดค์ run๋ฉ์๋๋ 1.5์ด๊ฐ ๋จ์๊ธฐ์ ์์ง ๋๊ธฐ ์ค์ธ ์ํ์ด๋ค ์ฆ ์ค๋ ๋๊ฐ ๋๋์ง ์์๋ค๋ ๋ป์ด๋ค. ์ด ์ํฉ์์ interrupt๊ฐ ๋ฐ์ํด์ ์์ธ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค. ๊ทธ๋์ join ๋ฉ์๋์ 500์ 2020์ผ๋ก ๊ธฐ๋ค๋ฆฌ๋ ์ด๋ฅผ ๋๋ ค์ค๋ค๋ฉด ์์ธ๊ฐ ๋ฐ์ํ์ง ์์ ๊ฒ์ด๋ค. ์ค๋ ๋์ 2์ด๋ฅผ ๊ธฐ๋ค๋ฆฐ ๋ค ์ค๋ ๋๊ฐ ์ข ๋ฃ๋๊ณ ์ธํฐ๋ฝํธ๊ฐ ๋ฐ์๋๋ ์๋ฌด๋ฐ ์์ธ๊ฐ ๋ฐ์ํ์ง ์์ ๊ฒ์ด๋ค.
๐ฏ Object ํด๋์ค์ ์ ์ธ๋ ์ค๋ ๋์ ๊ด๋ จ ์๋ ๋ฉ์๋๋ค
Thread ํด๋์ค์ ์ ์ธ๋ ๋ฉ์๋ ์ธ์ ์ค๋ ๋์ ์ํ๋ฅผ ํต์ ํ๋ Object ํด๋์ค๋ค์ ์ ์ธ๋์ด ์๋ ๋ฉ์๋๋ค์ด ์๋ค. ์ค๋ ๋(1)์์ synchronized์์ "๋ฌธ์ง๊ธฐ"๋ผ๋ ์์์ด๋ก ๋ฑ์ฅํ์๋ค.
๋ฆฌํด ํ์ | ๋ฉ์๋ ์ด๋ฆ ๋ฐ ๋งค๊ฐ ๋ณ์ | ์ค๋ช |
void | wait() | ๋ค๋ฅธ ์ค๋ ๋๊ฐ Object ๊ฐ์ฒด์ ๋ํ notify() ๋ฉ์๋๋ notifyAll() ๋ฉ์๋๋ฅผ ํธ์ถํ ๋๊น์ง ํ์ฌ ์ค๋ ๋๊ฐ ๋๊ธฐํ๊ณ ์๋๋กํ๋ค. |
void | wait(long timeout) | wait() ๋ฉ์๋์ ๋์ผํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฉฐ, ๋งค๊ฐ ๋ณ์์ ์ง์ ํ ์๊ฐ๋งํผ ๋๊ธฐํ๋ค. ์ฆ, ๋งค๊ฐ ๋ณ์ ์๊ฐ์ ๋์ด ์ฐ์ ๋์๋ ํ์ฌ ์ค๋ ๋๋ ๋ค์ ๊นจ์ด๋๋ค. |
void | wait(long timeout, int nanos) | wait() ๋ฉ์๋์ ๋์ผํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ํ์ง๋ง wait(timeout)์์ ๋ฐ๋ฆฌ์ด ๋จ์์ ๋๊ธฐ ์๊ฐ์ ๊ธฐ๋ค๋ฆฐ๋ค๋ฉด, ์ด ๋ฉ์๋๋ ๋ณด๋ค ์์ธํ ๋ฐ๋ฆฌ์ด + ๋๋ ธ์ด ๋งํผ๋ง ๋๊ธฐํ๋ค. |
void | notify() | Object ๊ฐ์ฒด์ ๋ชจ๋ํฐ์ ๋๊ธฐํ๊ณ ์๋ ๋จ์ผ ์ค๋ ๋๋ฅผ ๊นจ์ด๋ค. |
void | notifyAll() | Object ๊ฐ์ฒด์ ๋ชจ๋ํฐ์ ๋๊ธฐํ๊ณ ์๋ ๋ชจ๋ ์ค๋ ๋๋ฅผ ๊นจ์ด๋ค. |
์์ ๋ด์ฉ๋ค์ ๋ณด๋ฉด ํ๋ก์ธ์ค์ ์ค๋ ๋์ ๊ฐ๋ ์ด ์ ์กํ ์๋ค๋ฉด, ์ฝ๊ฒ ์ดํด๊ฐ ๋ ์ ์์ ๊ฒ์ด๋ค. 1์ฐจ์์ ์ผ๋ก ์ง์ ์ ์ผ๋ก ์ค๋ช ํ์๋ฉด, wait() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋ ๋๊ฐ ๋๊ธฐ ์ํ๊ฐ ๋๋ฉฐ, notify()๋ notifyAll() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ค๋ ๋์ ๋๊ธฐ ์ํ๊ฐ ํด์ ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package com.study;
class StateThread1 extends Thread {
private Object monitor;
public StateThread1(Object monitor) {
this.monitor = monitor;
}
@Override
public void run() {
try {
for(int i = 0; i < 10000; i++) {
String a = "A";
}
synchronized (monitor) {
monitor.wait();
}
System.out.println(getName() + " is notified");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// entry point
public class RunThreads {
public static void main(String[] args) {
RunThreads sample = new RunThreads();
sample.checkThreadState2();
}
public void checkThreadState2() {
Object monitor = new Object();
StateThread1 thread = new StateThread1(monitor);
try {
System.out.println("thread state : " + thread.getState());
thread.start();
System.out.println("thread state(after start) : " + thread.getState());
Thread.sleep(100);
System.out.println("thread state(after 0.1 sec) : " + thread.getState());
synchronized (monitor) {
monitor.notify();
}
Thread.sleep(100);
System.out.println("thread state(after notify) : " + thread.getState());
thread.join();
System.out.println("thread state(after join) : " + thread.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
|
cs |
๊ฒฐ๊ณผ๋ ์์์ ๋ฉ์๋ ์ค๋ช ์ ์ ์ดํดํ๋ค๋ฉด ์ฝ๊ฒ ์ดํด๊ฐ ๋ ๊ฒ์ด๋ค. ๋ค๋ง wait()๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด, ์ค๋ ๋์ ์ํ๊ฐ WAITING ์ํ๊ฐ ๋๋ค. ์ฆ ๊นจ์์ฃผ๊ธฐ ์ ๊น์ง๋ ๊ณ์ WAITING ์ํ๊ฐ ์ ์ง๊ฐ ๋๋ค. ์ด๋ interrupt() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋๊ธฐ ์ํ์์ ํ๋ ค๋ ์๋ ์๊ฒ ์ง๋ง, notify() ๋ฉ์๋๋ฅผ ํธ์ถํด์ ํ์ด์ผ InterruptedException๋ ๋ฐ์ํ์ง ์๊ณ , wait() ์ดํ์ ๋ฌธ์ฅ๋ ์ ์์ ์ผ๋ก ์ํํ๊ฒ ๋๋ค.
checkThreadState2() ๋ฉ์๋์ "StateThread1 thread2 = new StateThread1(monitor);" thread2 ๊ด๋ จ ๋ด์ฉ๋ค์ ์ถ๊ฐํ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public void checkThreadState2() {
Object monitor = new Object();
StateThread1 thread = new StateThread1(monitor);
StateThread1 thread2 = new StateThread1(monitor);
try {
System.out.println("thread state : " + thread.getState());
thread.start();
thread2.start();
System.out.println("thread state(after start) : " + thread.getState());
Thread.sleep(100);
System.out.println("thread state(after 0.1 sec) : " + thread.getState());
synchronized (monitor) {
monitor.notify();
}
Thread.sleep(100);
System.out.println("thread state(after notify) : " + thread.getState());
thread.join();
System.out.println("thread state(after join) : " + thread.getState());
thread2.join();
System.out.println("thread2 state(after join) : " + thread2.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
|
cs |
์ด๋ ๊ฒ ํ๊ฒ๋๋ฉด ์ข ๋ฃ๊ฐ ๋์ง ์๊ณ thread2๊ฐ ๊ณ์ ๋๊ธฐ์ํ๋ก ๋จ์์๊ฒ ๋๋ค. ์ด์ ๋ monitor.notify()๋ thread์ wait๋ง ๊นจ์ด๋ค. ์ฆ thread2์ wait๋ ๊นจ์์ง์ง ์์ ๊ฒ์ด๋ค. ์ ๊ธฐ์ monitor.notify()๋ฅผ ํ ์ค ๋ ์ถ๊ฐํด์ ํด๊ฒฐํ ์๋ ์์ง๋ง, wait() ์ํ๊ฐ ๋ช ๊ฐ์ธ์ง ๋ชจ๋ฅด๋ ์ํ์์๋ ์ ํฉํ ๋ฐฉ๋ฒ์ด ์๋๋ค.
์ด๋ด ๋์๋ notifyAll() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
๐ ๋ง๋ฌด๋ฆฌ
ํ์คํ ํ๋ก์ธ์ค์ ์ค๋ ๋๋ฅผ ์ด์์ฒด์ ํํธ๋ฅผ ์ง๊ณ ๋ค์ ์ค๋ ๋ ํํธ๋ฅผ ์ฝ๋๋ ๋ฒจ๋ก ๊ณต๋ถ๋ฅผ ํ๋ ์ดํด๊ฐ ๋ ๊น๊ฒ ์ ๋์๊ณ , ๋จธ๋ฆฟ์์ผ๋ก ๋ฉ์ปค๋์ฆ์ด ๊ทธ๋ ค์ ธ์ ๋ฟ๋ฏํ๋ค.
์ฐธ๊ณ
- [๊ต์ฌ] ์๋ฐ์์ (์ด์๋ฏผ ์ )
- [๊ต์ฌ] ๊ทธ๋ฆผ์ผ๋ก ๋ฐฐ์ฐ๋ ๊ตฌ์กฐ์ ์๋ฆฌ ์ด์์ฒด์ (๊ตฌํํ ์ )
- https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
๋ฐ์ํ
'SKILL > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comparator, ComparTo (0) | 2021.11.03 |
---|---|
์๋ฐ : Fork/Join framework (0) | 2021.09.09 |
์๋ฐ : ์ค๋ ๋(1) (0) | 2021.09.07 |
์๋ฐ : java 7 - ๋ณด์๋ ์์ธ ์ฒ๋ฆฌ (0) | 2021.09.03 |
์๋ฐ : Heap pollution ์ด๋? (0) | 2021.09.02 |