SKILL/JAVA

์ž๋ฐ” : ์Šค๋ ˆ๋“œ(2)

Jedy_Kim 2021. 9. 8. 19:54
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