IT/Android

Thead & Runnable

바바옄 2015. 6. 24. 11:07
반응형

Thread

 

- 독립적

- 클래스(extends)

- 스레드 감시는 다른 스레드가 (값 체크)

- 절대 여러개의 스레드가 하나의 자원을 바라보면 안됨! (static 이런 거 절대 쓰면안됨)

- 꼭 쓰고 싶다면, volatile 로

 

STX -> java 프로젝트 생성(ThreadExam)

 

ThreadTest.java

 

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
 
public class ThreadTest{
    
    public void startThread(){
        MyThread myThread = new MyThread();
        MyThread myThread2 = new MyThread();
        
        // 호출은 start        
        myThread.start();
        myThread2.start();
        
        Thread checkIValue = new CheckIValue(myThread, 50);
        checkIValue.start();
        Thread checkIValue2 = new CheckIValue(myThread2, 150);
        checkIValue2.start();
    }
    
    public static void main(String[] args){
        new ThreadTest().startThread();        
    }
    
    private class CheckIValue extends Thread{
        
        private MyThread myThread;
        private int maxValue;
        
        public CheckIValue(MyThread myThread, int maxValue){
            this.myThread = myThread;
            this.maxValue = maxValue;
        }
        
        @Override
        public void run(){
            
            while(true){
                try{
                    Thread.sleep(1);
                }catch(InterruptedException e){
                }
                
                if(myThread.i >= maxValue){
                    myThread.isRunning = false;
                    break;
                }
            }
        }
        
    }
    
    
    private class MyThread extends Thread{
        
        public volatile boolean isRunning = true;
        public volatile int i = 0;
        
        // 구현은 run
        @Override
        public void run() {    
            
            while(isRunning){    
            
                try{
                    Thread.sleep(1);
                }catch(InterruptedException e){                    
                }
                System.out.println(++i);
 
            }
                    
        }
    }
}
cs

 

 

 

 

 

Runnable

 

- Thread 클래스(extends), Runnable은 인터페이스(implements)

- Thread는 다른 Thread가 감시하지만 Runnable은 자기 자신이 감시해야 한다.(값 체크)

- 일반적으로 Runnable을 많이 사용한다.

 

 

RunnableTest.java

 

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
public class RunnableTest {
 
    public void startRunnable() {
        
        // Runnable은 보통 이렇게 사용
        new Thread(new MyThreadRunnable(200)).start();
        
        // 이런식으로 잘 사용 안함
        /*Thread thread = new Thread(new MyThreadRunnable(200));
        thread.start();*/
        //thread.stop(); 있긴 하지만 동작하지 않는다.
        
    }
 
    public static void main(String[] args) {
        new ThreadTest().startThread();
    }
    
    private class MyThreadRunnable implements Runnable {
 
        public  boolean isRunning = true;
        public  int i = 0;
        
        public int maxValue;
        
        public MyThreadRunnable(int maxValue) {
            this.maxValue = maxValue;
        }
 
        // 구현은 run
        @Override
        public void run() {
 
            while (isRunning) {
 
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                }
                System.out.println(++i);
                
                if(i>=maxValue){
                    break;
                }
 
            }
 
        }
    }
}
cs

 

 

 

 

thread와 runnable의 차이

 

- 스레드인데 다른 클래스를 상속받아야 할때 runnable (java는 다중상속이 불가능하기 때문에)

- 미세한 차이로 runnable이 약간 빠르다.

 

 

반응형

'IT > Android' 카테고리의 다른 글

Networking - API 요청하기  (0) 2015.06.24
Networking - 웹으로 요청하기  (0) 2015.06.24
1. Database를 이용한 전화번호부 실습  (0) 2015.06.23
Database  (0) 2015.06.23
Receiver  (0) 2015.06.23