프로그래밍/안드로이드

안드로이드 쓰레드 4번째 메시지 풀을 이용하는 방법(obtain 메소드)

가카리 2012. 8. 21. 21:23
반응형

쓰레드 4번째 메시지 풀을 이용하는 방법

 

Message객체를 계속 만들면 메모리도 많이 소모하고 속도도 느려집니다. 그래서 안드로이드는 메시지풀이라는 것을 두었습니다.

 

static Message obtain([Message orig]) 메시지풀에서 비슷한 메시지를 꺼내 사용하는것(일종의 new로 인스턴스 만드는 것랑 비슷함)

static Message obtain(Handler h, int what, int arg1, int arg2. Object obj)

void recycle() 사용한 메시지를 풀에 다시 집어 넣음

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

> 

<TextView

android:id="@+id/mainvalue"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20sp"

android:text="MainValue : 0"

/>

<TextView

android:id="@+id/backvalue"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20sp"

android:text="BackValue : 0"

/>

<Button

android:id="@+id/increase"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="증가"

/>

</LinearLayout>

 

 

 

 

자바파일

 

package com.android.ex92;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.TextView;

 

public class ex92 extends Activity {

       int mMainValue = 0;

       TextView mMainText;

       TextView mBackText;

       BackThread mThread;

 

       public void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);

             setContentView(R.layout.main);

 

             mMainText = (TextView)findViewById(R.id.mainvalue);

             mBackText = (TextView)findViewById(R.id.backvalue);

             mThread = new BackThread(mHandler);

             mThread.setDaemon(true);

             mThread.start();

       }

 

       public void mOnClick(View v) {

             mMainValue++;

             mMainText.setText("MainValue : " + mMainValue);

       }

 

       Handler mHandler = new Handler() {

       public void handleMessage(Message msg) {

             //handleMessage메소드를 구현하는 방법으로 쓰레드를 만듬

             if (msg.what == 0) {

                           mBackText.setText("BackValue : " + msg.arg1);

             }

       }

};

}

 

//3번째 방법과 비슷함

class BackThread extends Thread {

       int mBackValue = 0;

       Handler mHandler;

 

       BackThread(Handler handler) {

             mHandler = handler;

       }

 

       public void run() {

             while (true) {

                    mBackValue++;

                   

                    //원래는 new Message 만들고

                    //msg.what = 0; msg.arg1 = mBackValue 이런식으로 넣어주는 것이었음

                    Message msg = Message.obtain(mHandler, 0, mBackValue, 0);

                    //what = 0 arg1=mBackValue arg2=0 으로 메시지 객체를 만듬

                    mHandler.sendMessage(msg);//보맨

                   

                    try {

                          

                           Thread.sleep(1000);

                          

                    } catch (InterruptedException e) {;}

             }

       }

}

 

 

반응형