본문

170924(일) - Processes and Threads

Processes and Threads


Processes and Threads

- application에 다른 component가 없으면, 단일 thread 실행으로 새로운 linux process를 시작한다.

- 같은 application의 모든 component는 같은 프로세스와 스레드에서 실행된다. (main thread)

- 동일한 application에 대한 process가 이미 존재하는 경우, 같은 process내에서 실행되고 같은 thread를 사용

- component가 별도 process에서 실행되도록 할 수 있으며, 추가 thread create가능



Processes

- 특정 component가 속한 process를 control 해야하는 경우 manifest에서 수행 가능

<activity><service><receiver>, and <provider>

- android:process attribute 로 제어 지원

- 동일한 linux user id를 공유하고 동일한 certificates 로 sign된 경우 동일한 process에서 실행되도록 설정 가능

<application> 의 android:process 는 모든 component에 적용되는 default를 설정


- Android system은 memory가 부족하거나 다른 process가 필요한 시점에서 process kill을 결정 가능

- process kill 결정 시 user에 대한 relative한 weight를 가늠한다.

- ex) visible activities process > unvisible process

Processes and Application Lifecycle.



Threads

- application이 launched 되면 system은 thread를 만드는데, main thread(UI thread) 라고 부른다.

- user interface widgets, drawing event 등 dispatching 을 담당하기 때문에 매우 중요하다.

android.widget and android.view 와 상호작용

- special 상황일때, main thread는 UI thread가 아닐 수 있다. (Thread annotations)

Note: The build tools treat the @MainThread and @UiThread annotations as interchangeable, so you can call @UiThread methods from @MainThread methods, and vice versa. However, it's possible for a UI thread to be different from the main thread in the case of system apps with multiple views on different threads. Therefore, you should annotate methods associated with an app's view hierarchy with @UiThreadand annotate only methods associated with an app's lifecycle with @MainThread.


- same process에서 실행되는 모든 component는 UI thread에서 instance화 되며, each component에 대한 system call이 해당 thread에 전달.

- System callbacks (onKeyDown(), lifecycle callback method) 은 항상 UI thread 에서 실행

- ex) 

1. user가 화면의 button pressed 

2. app's UI thread에서 widgets에 touch event 발송

3. widget은 pressed된 상태를 설정하고, event queue에 invalidate request를 enqueue 한다.

4. UI thread가 이 요청을 dequeue하고 widget에 스스로 다시 draw 해야한다고 알린다.


- request and response interaction single thread model은 적절하게 구현하지 않으면 poor performance를 유발한다.

- 특히, UI thread에서 long operations (network access, db queries)을 수행하면 UI 가 block 된다.

- Thread가 block되면, no event dispatched and drawing event.

- 이상태로 5 second가 지나면, Application Not Responding (ANR) 이 발생한다.


1. Do not block the UI thread.

2. Do not access the Android UI toolkit form outside the UI thread.



worker threads

- should make sure to do separate threads (background or worker).

- separate threads 에서 UI 접근은 불가능 하므로 아래 방법을 쓴다.

public void onClick(View v) {
   
new Thread(new Runnable() {
       
public void run() {
           
// a potentially  time consuming task
           
final Bitmap bitmap =
                    processBitMap
("image.png");
            mImageView
.post(new Runnable() {
               
public void run() {
                    mImageView
.setImageBitmap(bitmap);
               
}
           
});
       
}
   
}).start();
}

- 코드 복잡성 제거를 위하여 thread에서 Handler를 사용하여 UI thread에 message를 전달하는 방법을 고려

- 가장 좋은 방법은 AsyncTask class를 extend하여 사용하는 것이다. 약파네



Thread-safe methods

- 2개 이상의 thread에서 같은 methods를 호출 할 수 있으므로 thread-safe 해야 한다.

- remote로 호출할 수 있는 method에 적합. (bound service.)

- 이미 호출 중인 method를 다른 thread에서 호출하면, 동일 process내의 thread pool에서 선택된 스레드에서 실행


IBinder에서 implement된 method의 call이 동일한 process에서 시작 된 경우 method는 caller의 thread에서 실행.

- but, call이 다른 process에서 시작된 경우 method는 IBinder와 동일한 프로세스에서 system이 maintain하는 thread pool에서 선택된 thread에서 실행

- Service는 둘 이상의 client를 가질 수 있기 때문에 둘 이상의 pool thread가 동일한 IBinder를 사용 가능하다.

∴ IBinder method는 thread로부터 안전해야 구현 가능하다.


- 유사하게 Content provider는 다른 process에서 시작된 data request를 받을 수 있다.

- precess의 UI thread가 아니라 Content provider의 process에 있는 thread pool에서 가져온다.

공유

댓글