[챕터 2-8-1] 스레드

2020. 2. 18. 23:49Android/Android 챕터 2-8

반응형

결과화면

스레드 시작 버튼 클릭 시 1씩 증가되며 표시

 

 


프로젝트명 : MyThread

 


MainActivity.java

더보기
package org.minokuma.mythread;

import androidx.appcompat.app.AppCompatActivity;

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

import org.w3c.dom.Text;

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 v) {
                BackgroundThread backgroundThread = new BackgroundThread();
                backgroundThread.start();
            }
        });
    }

    class BackgroundThread extends Thread {
        int value = 0;
        public void run(){
            for(int i = 0; i < 100; i++ ){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                value += 1;
                Log.d("MyThread", "value : " + value);
                textView.setText("값 : " + value);
            }
        }
    }
}

MainActivity.java

더보기
package org.minokuma.mythread;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

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

import org.w3c.dom.Text;

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 v) {
                BackgroundThread backgroundThread = new BackgroundThread();
                backgroundThread.start();
            }
        });
    }

    class BackgroundThread extends Thread {
        int value = 0;
        public void run(){
            for(int i = 0; i < 100; i++ ){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                value += 1;
                Log.d("MyThread", "value : " + value);
                textView.setText("값 : " + value);
            }
        }
    }

    class MainHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    }

}


MainActivity.java

 

더보기
package org.minokuma.mythread;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    TextView textView;
    Handler handler = new Handler();

    @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 v) {
                BackgroundThread backgroundThread = new BackgroundThread();
                backgroundThread.start();
            }
        });
    }

    class BackgroundThread extends Thread {
        int value = 0;
        public void run(){
            for(int i = 0; i < 100; i++ ){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                value += 1;
                Log.d("MyThread", "value : " + value);

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("값1 : " + value);
                    }
                });
                /*
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("값2 : " + value);
                    }
                }, 5000);
                */
            }
        }
    }
}

 

 

반응형

'Android > Android 챕터 2-8' 카테고리의 다른 글

[챕터 2-8-2] 동기화  (0) 2020.02.19