안녕하세요 GG 임돠

이번에 쓰레드 임돠

 

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
package com.javac.im;
 
class ThreadEx9 {
    public static void main(String args[]) {
        ThreadEx9_1 th1 = new ThreadEx9_1();
        ThreadEx9_2 th2 = new ThreadEx9_2();
 
        th2.setPriority(7);//thread 7번 실행
 
        System.out.println("Priority of th1(-) : " + th1.getPriority() );
        System.out.println("Priority of th2(|) : " + th2.getPriority() );
        th1.start();
        th2.start();
    }
}
 
class ThreadEx9_1 extends Thread {//Thread 상속받아 사용
    public void run() {
        for(int i=0; i < 20; i++) {
            System.out.print("-");
            for(int x=0; x < 10000000; x++);//waiting 
        }
    }
}
 
class ThreadEx9_2 extends Thread {
    public void run() {
        for(int i=0; i < 20; i++) {
            System.out.print("|");
            for(int x=0; x < 10000000; x++);//waiting
        }
    }
}
cs

 

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.javac.im;
class ThreadEx26 {
    public static void main(String args[]) {
        MyThreadEx26 th1 = new MyThreadEx26("*");
        MyThreadEx26 th2 = new MyThreadEx26("**");
        MyThreadEx26 th3 = new MyThreadEx26("***");
        th1.start();
        th2.start();
        th3.start();
 
        try {
            Thread.sleep(2000);
            th1.suspend();
            Thread.sleep(2000);
            th2.suspend();
            Thread.sleep(3000);
            th1.resume();
            Thread.sleep(3000);
            th1.stop();
            th2.resume();
            th2.stop();
            Thread.sleep(2000);
            th3.stop();
        } catch (InterruptedException e) {}
    }
}
 
class MyThreadEx26 implements Runnable {
    static final int RUNNING = 0;
    static final int SUSPENDED = 1;
    static final int STOPPED = 2;
 
    private int state = RUNNING;
    Thread th;
 
    MyThreadEx26(String name) {
        th = new Thread(this, name); // Thread(Runnable r, String name)
    }
 
    public synchronized void setState(int state) {
        this.state = state;
 
        // state가 SUSPENDED였다가 RUNNING으로 변경되면, nofity()를 호출한다.
        if(state==RUNNING) {
            notify();
        } else {
            th.interrupt();
        }
    }
 
    public synchronized boolean checkState() {
        // state가 SUSPENDED면 wait()을 호출해서 쓰레드를 대기상태로 만들고,
        while(state==SUSPENDED) {
            try {
                wait();
            } catch(InterruptedException e) {}
        }
        // state가 STOPPED이면 true를, 그 외에는 false를 리턴한다.
        return state==STOPPED;
    }
 
    public void run() {
        String name = Thread.currentThread().getName();
 
        while(true) {
            System.out.println(name);
            try {
                Thread.sleep(1000);
            } catch(InterruptedException e) {}
 
            // state가 STOPPED이면 checkState()가 true를 반환해서 
            // while문을 벗어나게 된다.
            if(checkState()) {
                break;
            }
        } // while
        System.out.println(name + " - END");
    } // run()
 
    public void suspend() {
        setState(SUSPENDED);
    }
 
    public void resume() {
        setState(RUNNING);
    }
 
    public void stop() {
        setState(STOPPED);
    }
 
    public void start() {
        th.start();
    }
}
cs

 

감사합니다.

+ Recent posts