본문 바로가기

Study/Programming

안드로이드 프로세스와 스레드 (스레드)

졸려


안녕하세요 꼬로미입니다!


안드로이드 프로세스와 스레드 이어지는 포스팅입니다


전 포스팅에서는 프로세스에 대해서 알아보았고 (프로세스 편 : http://colomy.tistory.com/49)


이번 포스팅에서는 스레드에 대해서 알아볼게요~




스레드(Thread)

애플리케이션이 시작되면 안드로이드 시스템은 애플리케이션을 위한 스레드를 생성, 

이 스레드를 메인 스레드(main thread)라고 하며

메인 스레드는 사용자 인터페이스 위젯으로 이벤트를 전달, 화면을 그리는 작업 담당

UI 스레드(user interface thread)라고도 불림


 



메인 스레드

동일한 프로세스 안에서 실행되는 모든 컴포넌트는 UI 스레드에서 실행

컴포넌트의 시스템 호출이 실행되는 스레드

사용자 인터페이스와 관련 콜백 메소드, 생애주기 관련 메소드들이 실행되는 스레드



작업 스레드(worker thread)

부담이 큰 작업들을 여러가지 실행할때 메인 스레드의 블록상태를 예방을 위해

스레드를 별도로 생성, 이를 작업스레드, 배경스레드 라고 부릅니다


다중스레딩 생성 방법 2가지

  • Thread 클래스를 상속받아 스레드를 작성하는 방법

  • Runnable 인터페이스 구현 후 Thread 객체에 전달하는 방법




- Thread 클래스를 상속받아 스레드 작성 예제


  1. package com.example.testfile;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6.  
  7. public class MainActivity extends Activity {
  8.     WorkerThread w;
  9.     boolean running = true;
  10.    
  11.     class WorkerThread extends Thread{
  12.         public void run() {
  13.             int i = 0;
  14.             for (i=0; i<20 && running; i++){
  15.                 try{
  16.                     Thread.sleep(1000);
  17.                 } catch(InterruptedException e) {
  18.                 }
  19.                 Log.v("THREAD", "time=" +i);
  20.             }
  21.         }
  22.     }
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         // TODO Auto-generated method stub
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.main);
  29.     }
  30.  
  31.     @Override
  32.     protected void onStart() {
  33.         // TODO Auto-generated method stub
  34.         super.onStart();
  35.         w = new WorkerThread();
  36.         running = true;
  37.         w.start();
  38.     }
  39.  
  40.     @Override
  41.     protected void onStop() {
  42.         // TODO Auto-generated method stub
  43.         super.onStop();
  44.         running = false;
  45.     }
  46. }



실행화면

time=1 텍스트부터 1초마다 로그가 하나씩 올라와서 time=19까지 가는 것을 볼 수 있음





-Runnable 인터페이스를 구현해 Thread 객체에 전달하는 예제

 

  1. package com.example.testfile;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6.  
  7. public class MainActivity extends Activity {
  8.     Thread w;
  9.     boolean running = true;
  10.    
  11.  
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         // TODO Auto-generated method stub
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.     }
  18.  
  19.     @Override
  20.     protected void onStart() {
  21.         // TODO Auto-generated method stub
  22.         super.onStart();
  23.         w = new Thread(new Runnable(){
  24.             public void run() {
  25.                 int i = 0;
  26.                 for (i=0; i<30 && running; i++){
  27.                     try{
  28.                         Thread.sleep(1000);
  29.                     } catch(InterruptedException e) {
  30.                     }
  31.                     Log.v("THREAD", "time=" +i);
  32.                 }
  33.             }
  34.         });
  35.         running = true;
  36.         w.start();
  37.     }
  38.  
  39.     @Override
  40.     protected void onStop() {
  41.         // TODO Auto-generated method stub
  42.         super.onStop();
  43.         running = false;
  44.     }
  45. }

이 클래스는 한번밖에 사용되지 않음으로

Runnable 인터페이스를 구현한 클래스를 무명클래스로 처리





Bye


읽어주셔서 감사드립니다^^


손가락버튼 한번씩 꾸욱 부탁드려요~