반응형

스레드 클래스를 상속받는 클래스를 정의하고 사용해 보자.

 

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
import time
import threading
 
class Worker(threading.Thread):
    def __init__(self, name, count, delay):
        super().__init__()
        self.name = name
        self.count = count
        self.delay = delay
 
    # 스레드 클래스를 상속하는 클래스는 run()를 재정의 해야 한다.
    # 객체를 만들고 start()를 실행하면 run()가 실행된다.
    def run(self):
        print(f"{self.name} job started.")
        for i in range(self.count):
            print(f"{self.name} job: {i}.")
            time.sleep(self.delay)
        print(f"{self.name} job finished.")
 
            
print("Main started.")
 
thread_1 = Worker("First"50.5)
#thread_1.daemon = True
# 데몬 스레드로 설정되면 메인 스레드 종료시 서브 스레드도 종료된다.
thread_1.start()
#thread_1.join()
# join()을 실행한 스레드가 종료할 때까지 나머지 스레드는 대기한다.
 
thread_2 = Worker("Second"50.5)
#thread_2.daemon = True
thread_2.start()
#thread_2.join()
 
print(f"■ Number of threads: {threading.active_count()}")
 
time.sleep(1)
 
print("Main finished.")
 

 

 

 

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
import time
import threading
 
class Worker(threading.Thread):
    def __init__(self, name, count, delay):
        super().__init__()
        self.name = name
        self.count = count
        self.delay = delay
        
    def run(self):
        print(f"{self.name} job started.")
        for i in range(self.count):
            print(f"{self.name} job: {i}.")
            time.sleep(self.delay)
        print(f"{self.name} job finished.")
 
thread_1 = Worker("First"50.5)
thread_2 = Worker("Second"50.5)
thread_3 = Worker("Third"50.5)
threads = [thread_1, thread_2, thread_3]
 
print(f"■ Number of threads: {threading.active_count()}")
# 활성화된 스레드는 메인스레드 뿐이므로 1이 표시된다.
 
for thread in threads:
    thread.start()
    thread.join()
 

 

 

 

thread - Thread-based parallelism

 

반응형
Posted by J-sean
:
반응형

멀티 스레드를 사용해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import time
import threading
 
def Job(name, count, delay):
    print(f"{name} job started.")
    for i in range(count):
        print(f"{name} job: {i}.")
        time.sleep(delay)
    print(f"{name} job finished.")
 
print("Main started.")
 
thread_1 = threading.Thread(target=Job, args=("First"50.5))
thread_1.start()
 
thread_2 = threading.Thread(target=Job, args=("Second"50.5))
thread_2.start()
 
print(f"■ Number of threads: {threading.active_count()}")
 
time.sleep(1)
 
print("Main finished.")
 

 

 

■ threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

- target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
- args is a list or tuple of arguments for the target invocation. Defaults to ().

 

 

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
import time
import threading
 
def Job(name, count, delay):
    print(f"{name} job started.")
    for i in range(count):
        print(f"{name} job: {i}.")
        time.sleep(delay)
    print(f"{name} job finished.")
 
print("Main started.")
 
thread_1 = threading.Thread(target=Job, args=("First"50.5))
thread_1.daemon = True
thread_1.start()
 
thread_2 = threading.Thread(target=Job, args=("Second"50.5))
thread_2.daemon = True
thread_2.start()
 
print(f"■ Number of threads: {threading.active_count()}")
 
time.sleep(1)
 
print("Main finished.")
 

 

 

 

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
import time
import threading
 
def Job(name, count, delay):
    print(f"{name} job started.")
    for i in range(count):
        print(f"{name} job: {i}.")
        time.sleep(delay)
    print(f"{name} job finished.")
 
print("Main started.")
 
thread_1 = threading.Thread(target=Job, args=("First"50.5))
thread_1.start()
thread_1.join()
 
thread_2 = threading.Thread(target=Job, args=("Second"50.5))
thread_2.start()
thread_2.join()
 
print(f"■ Number of threads: {threading.active_count()}")
 
time.sleep(1)
 
print("Main finished.")
 

 

 

■ join(timeout=None)

- Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.
- When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.
- When the timeout argument is not present or None, the operation will block until the thread terminates.
- A thread can be joined many times.

 

 

반응형
Posted by J-sean
:
반응형

프로그램의 어떤 함수가 컴퓨터 CPU 자원을 독점하는지 찾아보자.

 

프로세스 익스플로러를 실행한다.

 

Options - Configure Symbols... 를 선택한다.

 

Dbghelp.dll 경로와 분석할 프로그램 Symbols 경로를 지정한다.

 

CPU 자원을 많이 소비하고 있는 프로세스를 찾는다.

 

 

우클릭 - Properties... 를 선택한다.

 

Threads 탭을 선택하고 CPU를 많이 사용하는 스레드를 찾아 선택하고 Stack 버튼을 클릭한다.

 

ThreadFunc2 함수가 실행되고 있음을 확인한다.

 

ThreadFunc2()

 

반응형
Posted by J-sean
:
반응형

Message, Handler, Looper를 이용해 메인 스레드에서 다른 스레드로 메세지(데이터)를 보내자.

 

레이아웃에 에디트텍스트, 버튼, 텍스트뷰를 적당히 배치한다.

 

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
package com.example.myapplication;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    TextView textView;
 
    MyThread myThread;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String input = editText.getText().toString();
                Message message = Message.obtain();
                message.obj = input;
 
                myThread.handler.sendMessage(message);
            }
        });
 
        myThread = new MyThread();
        myThread.start();
    }
 
    class MyThread extends Thread {
        Handler handler;
 
        @Override
        public void run() {
            handler = new Handler(Looper.getMainLooper()) {
                @Override
                public void handleMessage(@NonNull Message msg) {
                    textView.setText(msg.obj + " from My Thread.");
                    // 메인 스레드의 메인 루퍼를 사용하기 때문에
                    // textView()에 바로 접근 가능하다.
                }
            };
        }
    }
}
 

 

메인 루퍼를 이용하는 소스를 작성하고 빌드한다.

 

에디트텍스트에 메세지를 입력하고 버튼을 터치하면 텍스트뷰에 표시된다.

 

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
package com.example.myapplication;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    TextView textView;
 
    MyThread myThread;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String input = editText.getText().toString();
                Message message = Message.obtain();
                message.obj = input;
 
                myThread.handler.sendMessage(message);
            }
        });
 
        myThread = new MyThread();
        myThread.start();
    }
 
    class MyThread extends Thread {
        Handler handler;
 
        @Override
        public void run() {
            Looper.prepare();
 
            handler = new Handler(Looper.myLooper()) {
                // 메인 스레드가 아닌 새로 만든 이 스레드의 루퍼 사용
                @Override
                public void handleMessage(@NonNull Message msg) {
                    String message = msg.obj.toString();
                    // 새로 만든 이 스레드로 넘어온 메세지 데이터를 미리 처리.
                    // 아래 setText()에서 msg.obj를 사용하면 null 값이 대입된다.
 
                    textView.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(message + " from My Thread");
                        }
                    });
                }
            };
 
            Looper.loop();
        }
    }
}
 

 

메인 루퍼가 아닌 새로 만든 스레드의 루퍼를 생성하고 메세지를 처리하는 소스. 결과는 동일하다.

 

 

(루퍼 종료용)버튼을 하나 더 추가한다.

 

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
package com.example.myapplication;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    TextView textView;
 
    MyThread myThread;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);
        
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String input = editText.getText().toString();
                Message message = Message.obtain();
                message.obj = input;
 
                myThread.myHandler.sendMessage(message);
            }
        });
 
        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myThread.myHandler.StopLooper();
                // 새로 만든 스레드의 루퍼 종료.
            }
        });
 
        myThread = new MyThread();
        myThread.start();
    }
 
    class MyThread extends Thread {
        MyHandler myHandler;
 
        @Override
        public void run() {
            Looper.prepare();
 
            myHandler = new MyHandler();
 
            Looper.loop();
        }
 
        class MyHandler extends Handler {
            MyHandler() {
                super(Looper.myLooper());
            }
 
            @Override
            public void handleMessage(@NonNull Message msg) {
                String message = msg.obj.toString();
                // 새로 만든 이 스레드로 넘어온 메세지 데이터를 미리 처리.
                // 아래 setText()에서 msg.obj를 사용하면 null 값이 대입된다.
 
                textView.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText(message + " from My Thread");
                    }
                });
            }
 
            void StopLooper() {
                myHandler.getLooper().quitSafely();
                // 새로 만든 스레드의 루퍼 종료.
            }
        }
    }
}
 

 

새 스레드의 루퍼를 종료하는 함수가 포함된 파생 Handler 클래스를 사용하는 소스.

Looper.XXX() 호출 시 MyThread 클래스 내부라도 어디서 루퍼를 참조 하느냐에 따라 메인 루퍼가 참조 될 수도 있고 새 스레드의 루퍼가 참조 될 수 있다. (MyThread.Run()에서만 새 스레드의 루퍼가 참조된다)

 

스레드 루퍼 종료 버튼 터치 후 다시 스레드로 보내기 버튼을 터치하면 아래와 같은 경고 발생.

 

새 스레드의 루퍼가 종료(별 다른 작업이 없다면 동시에 새 스레드도 종료) 된 후 핸들링할 메세지를 보내기 때문에 경고가 발생한다. (예외 처리 필요)

 

※ 참고

Looper

 

반응형
Posted by J-sean
:
반응형

안드로이드 UI는 메인 스레드나 UI 스레드 외 다른 스레드에서 조작 할 수 없다. 다른 스레드에서 UI 조작 시 별다른 에러 없이 Virtual Device에서 잘 작동 되더라도 실제 기기에서는 종료되어 버린다. 아니면 테스트 시 Logcat에 아래와 같은 에러가 표시된다.

 

CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

 

텍스트뷰와 버튼을 적당히 배치한다.

 

다른 스레드에서 텍스트뷰에 접근하는 코드를 작성한다.

 

위 예제의 경우 AVD에서는 별 이상없이 동작 하지만 실제 기기에서 버튼을 터치하면 바로 종료되어 버린다. 메인스레드나 UI 스레드가 아닌 다른 스레드에서 텍스트뷰에 접근하기 때문이다.

 

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
package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    TextView textView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView = findViewById(R.id.textView);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyThread thread = new MyThread();
                thread.start();
            }
        });
    }
 
    class MyThread extends Thread {
        @Override
        public void run() {
            textView.post(new Runnable() {
                @Override
                public void run() {
                    textView.setText("My Thread.");
                }
            });
        }
    }
}
 

 

위와 같이 소스를 수정하고 빌드한다.

 

문제없이 잘 실행된다.

 

 

1
2
3
4
5
6
7
8
9
10
11
class MyThread extends Thread {
    @Override
    public void run() {
        textView.postDelayed(new Runnable() {
            @Override
            public void run() {
                textView.setText("My Thread");
            }
        }, 5000);
    }
}
 

 

.post()가 아닌 .postDelayed()를 사용하면 Thread.sleep()를 사용하지 않고 일정시간 후 실행할 수 있다. 위 예제는 버튼 터치 5초 후 텍스트를 바꾼다.

 

다른 스레드에서 UI 스레드에 접근 하는 방법은 여러가지가 있다. 아래 링크를 참고하자.

  • Activity.runOnUiThread(Runnable)
  • View.post(Runnable)
  • View.postDelayed(Runnable, long)

프로세스 및 스레드 개요

 

반응형
Posted by J-sean
: