반응형

 

package com.example.mythread;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    int value = 0;
    TextView textView;

    MainHandler handler = new MainHandler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        textView = findViewById(R.id.tv);

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                BackgroundThread thread = new BackgroundThread();
                thread.start();
            }
        });
    }


    // 새로 생긴 쓰레드가 하는 일 지정
    class BackgroundThread extends Thread{
        public void run(){
            for(int i=0; i<100; i++){
                try{
                    Thread.sleep(100);
                }catch (Exception e){
                    Log.d("Thread", "run: " + e.toString());
                }
                value += 1;
                Log.d("Thread", "value: " + value);



                // 메시지 객체를 받아와서
                Message message = handler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putInt("value" , value);
                message.setData(bundle);

                // 메시지 큐에 sendMessege를 한다.
                handler.sendMessage(message);
            }
        }
    }


    //메인쓰레드가 한다
    class MainHandler extends Handler {
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);

            Bundle bundle = msg.getData();
            int value = bundle.getInt("value");
            textView.setText("value 값 " + value);
        }

    }

}

 

 

 

handler의 post() 메소드를 이용하여 새로만든 Runnable 객체를 전달해 주면

이 객체에 정의된 run() 메소드 내의 코드들은 메인 스레드에서 실행된다.

이 경우에 Bundle을 이용해서 데이터를 주고받지 않아도

간결하게 코드를 작성할 수 있다.

package com.example.mythread;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    int value = 0;
    TextView textView;

    MainHandler handler = new MainHandler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        textView = findViewById(R.id.tv);

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                BackgroundThread thread = new BackgroundThread();
                thread.start();
            }
        });
    }


    // 새로 생긴 쓰레드가 하는 일 지정
    class BackgroundThread extends Thread{
        public void run(){
            for(int i=0; i<100; i++){
                try{
                    Thread.sleep(100);
                }catch (Exception e){
                    Log.d("Thread", "run: " + e.toString());
                }
                value += 1;
                Log.d("Thread", "value: " + value);


                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("value 값: " + value);
                    }
                });
            }
        }
    }


    //메인쓰레드가 한다
    class MainHandler extends Handler {
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);

//            Bundle bundle = msg.getData();
//            int value = bundle.getInt("value");
//            textView.setText("value 값 " + value);
        }

    }

}

 

+ Recent posts