안드로이드

 

 

class MainActivity

package com.example.choi.my21;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    }


    public void  onClick(View v){

        Intent intent=null;
        switch (v.getId()){

            case R.id.button1:
                intent=new Intent(MainActivity.this, ThreadOneActivity.class);
                break;

            case R.id.button2:
                intent=new Intent(MainActivity.this, ThreadTwoActivity.class);
                break;

            case R.id.button3:
                intent=new Intent(MainActivity.this, ThreadThresActivity.class);
                break;

            case R.id.button4:
                intent=new Intent(MainActivity.this, ThreadFourActivity.class);
                break;

            case R.id.button5:
                intent=new Intent(MainActivity.this, ThreadFiveActivity.class);
                break;

        }
        startActivity(intent);
    }




}

 

R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.choi.my21.MainActivity">

    <Button
        android:text="Thread 객체에 run메서드 구현"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/button1"
        android:textAllCaps="false"
        android:background="@android:color/holo_green_dark"
        android:textColor="@android:color/background_light"
        android:onClick="onClick"
        />

    <Button
        android:text="Runnable객체의 run메소드 구현"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:id="@+id/button2"
        android:textAllCaps="false"
        android:background="@android:color/holo_blue_light"
        android:textColor="@android:color/background_light"
        android:onClick="onClick"
        />

    <Button
        android:text="핸들러"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="23dp"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/holo_orange_dark"
        android:textColor="@android:color/background_light"
        android:onClick="onClick"
        />

    <Button
        android:text="메시지"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp"
        android:id="@+id/button4"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/holo_red_dark"
        android:textColor="@android:color/background_light"
        android:onClick="onClick"
        />

    <Button
        android:text="runOnUiThread 메서드"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="21dp"
        android:id="@+id/button5"
        android:textAllCaps="false"
        android:layout_below="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="?attr/colorPrimary"
        android:textColor="@android:color/background_light"
        android:onClick="onClick"
        />


</RelativeLayout>

 

 

 

 

 

1. Thread 객체에 run 메서드 구현

 


    class NewThread extends Thread{
        @Override
        public void run() {

            while (isThread){
                secondNum++;
                try{
                    //0.5초
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i("ThreadOneActivity", " "+secondNum);

            }

        }
    }

 

 

class ThreadOneActivity
package com.example.choi.my21;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ThreadOneActivity extends AppCompatActivity {

    private int mainNum;
    private int secondNum;
    private TextView tvMain, tvSecond;
    private Button btnStart;

    private Button stopThread;

    private boolean isThread=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thread_one);

        tvMain=(TextView)findViewById(R.id.tv_main_thread);
        tvSecond=(TextView)findViewById(R.id.tv_second_thread);
        btnStart=(Button)findViewById(R.id.bt_start);
        stopThread=(Button)findViewById(R.id.stopThread);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=true;
                startNum();
            }
        });


        stopThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=false;

                Toast.makeText(ThreadOneActivity.this, "쓰레드 종료", Toast.LENGTH_SHORT).show();
            }
        });

    }


    private void startNum(){
        mainNum++;

        NewThread newThread=new NewThread();
        newThread.setDaemon(true);
        newThread.start();

        tvMain.setText("mainNum : "+ mainNum);
        tvSecond.setText("secondNum : " +secondNum);
    }


    class NewThread extends Thread{
        @Override
        public void run() {

            while (isThread){
                secondNum++;
                try{
                    //0.5초
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i("ThreadOneActivity", " "+secondNum);

            }

        }
    }





}





 

R.layout.thread_one
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/thread_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.choi.my21.ThreadOneActivity">

    <TextView
        android:text="Thread 객체에 run 메서드 구현"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="normal|bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:id="@+id/tv_second_thread"
        android:textStyle="normal|bold"
        android:textAlignment="center"
        android:textSize="30sp"
        android:textColor="@color/colorAccent"

        android:layout_below="@+id/tv_main_thread"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="Start Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="118dp"
        android:id="@+id/bt_start"
        android:background="@android:color/holo_green_light"
        android:textColor="@android:color/background_light"
        android:textAllCaps="false"
        android:layout_below="@+id/tv_second_thread"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_main_thread"
        android:textStyle="normal|bold"
        android:textAlignment="center"
        android:textSize="30sp"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_marginTop="72dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="쓰레드 종료"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="119dp"
        android:layout_marginEnd="119dp"
        android:id="@+id/stopThread"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@android:color/holo_red_dark"
        android:textColor="@android:color/background_light" />


</RelativeLayout>

 

 

 

2. Runnable 객체에 run 메서드 구현


    class SecondRunnable implements Runnable{
        @Override
        public void run() {
            while (isThread){
                secondNum++;
                try{
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }

                Log.i("ThreadTwoActivity", " "+secondNum);
            }
        }
    }

 

 

class ThreadTwoActivity
package com.example.choi.my21;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ThreadTwoActivity extends AppCompatActivity {

    private int mainNum;
    private int secondNum;
    private TextView tvMain, tvSecond;
    private Button btnStart, stopThread;
    private boolean isThread=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thread_two);

        tvMain=(TextView)findViewById(R.id.tv_main_thread);
        tvSecond=(TextView)findViewById(R.id.tv_second_thread);

        btnStart=(Button)findViewById(R.id.bt_start);
        stopThread=(Button)findViewById(R.id.stopThread);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=true;
                startNum();
            }
        });

        stopThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=false;
                Toast.makeText(ThreadTwoActivity.this, "쓰레드 종료", Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void startNum(){
        mainNum++;
        SecondRunnable runnable=new SecondRunnable();
        Thread newThread=new Thread(runnable);
        newThread.setDaemon(true);
        newThread.start();
        tvMain.setText("mainNum: "+mainNum);
        tvSecond.setText("secondNum : " +secondNum);
    }


    class SecondRunnable implements Runnable{
        @Override
        public void run() {
            while (isThread){
                secondNum++;
                try{
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }

                Log.i("ThreadTwoActivity", " "+secondNum);
            }
        }
    }


}







 

R.layout.thread_two
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/thread_two"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.choi.my21.ThreadTwoActivity">

    <TextView
        android:text="Runnable객체의 run메소드 구현"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="normal|bold" />



    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:id="@+id/tv_second_thread"
        android:textStyle="normal|bold"
        android:textAlignment="center"
        android:textSize="30sp"
        android:textColor="@color/colorAccent"

        android:layout_below="@+id/tv_main_thread"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="Start Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="118dp"
        android:id="@+id/bt_start"
        android:background="@android:color/holo_green_light"
        android:textColor="@android:color/background_light"
        android:textAllCaps="false"
        android:layout_below="@+id/tv_second_thread"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_main_thread"
        android:textStyle="normal|bold"
        android:textAlignment="center"
        android:textSize="30sp"
        android:textColor="@android:color/holo_blue_dark"
        android:layout_marginTop="72dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="쓰레드 종료"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="119dp"
        android:layout_marginEnd="119dp"
        android:id="@+id/stopThread"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@android:color/holo_red_dark"
        android:textColor="@android:color/background_light" />


</RelativeLayout>

 

 

3.핸들러

 

 

 

 

 Handler mainHandler=new Handler();


   class NewThread extends Thread{
        @Override
        public void run() {
            while (isThread){
                secondNum++;

                Log.i(TAG, "secondNum in thread : " + secondNum);

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        tvSecond.setText("secondNum :" + secondNum);
                    }
                });

                try {
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    }

 

class ThreadThresActivity
package com.example.choi.my21;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ThreadThresActivity extends AppCompatActivity {

    final static String TAG="ThreadThresActivity";

    private int mainNum;
    private int secondNum;
    private TextView tvMain, tvSecond;
    private Button btnStart;

    private Button stopThread;

    private boolean isThread=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thread_thres);


        tvMain=(TextView)findViewById(R.id.tv_main_thread);
        tvSecond=(TextView)findViewById(R.id.tv_second_thread);
        btnStart=(Button)findViewById(R.id.bt_start);
        stopThread=(Button)findViewById(R.id.stopThread);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=true;
                startNum();
            }
        });


        stopThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=false;
                Toast.makeText(ThreadThresActivity.this, "쓰레드 종료", Toast.LENGTH_SHORT).show();
            }
        });

    }



    private void startNum(){
        mainNum++;

        NewThread newThread=new NewThread();
        newThread.setDaemon(true);
        newThread.start();

        tvMain.setText("mainNum :" + mainNum);

    }


    Handler mainHandler=new Handler();


    class NewThread extends Thread{
        @Override
        public void run() {
            while (isThread){
                secondNum++;

                Log.i(TAG, "secondNum in thread : " + secondNum);

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        tvSecond.setText("secondNum :" + secondNum);
                    }
                });

                try {
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    }



}




 

 

 

4.메시지

 

 


    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0){
                tvSecond.setText("secondNum : " + secondNum);
                Log.i(TAG, "secondNum in handler :" + secondNum);
            }
        }
    };



    class NewThread extends Thread{

        @Override
        public void run() {
            while (isThread){
                secondNum++;
                Log.i(TAG, "secondNum in thread :" + secondNum);

                try {
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }

                Message msg =new Message();
                msg=Message.obtain();
                msg.what=0;
                msg.arg1=0;
                msg.arg2=0;
                msg.obj=null;
                handler.sendMessage(msg);
                handler.sendEmptyMessage(0);
            }
        }
    }

 

class ThreadFourActivity
package com.example.choi.my21;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ThreadFourActivity extends AppCompatActivity {

    final static String TAG="ThreadFourActivity";

    private int mainNum;
    private int secondNum;
    private TextView tvMain, tvSecond;
    private Button btnStart;

    private Button stopThread;

    private boolean isThread=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thread_four);


        tvMain=(TextView)findViewById(R.id.tv_main_thread);
        tvSecond=(TextView)findViewById(R.id.tv_second_thread);
        btnStart=(Button)findViewById(R.id.bt_start);
        stopThread=(Button)findViewById(R.id.stopThread);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=true;
                startNum();
            }
        });


        stopThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=false;
                Toast.makeText(ThreadFourActivity.this, "쓰레드 종료", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void startNum(){
        mainNum++;

        NewThread newThread=new NewThread();
        newThread.setDaemon(true);
        newThread.start();

        tvMain.setText("mainNum :" + mainNum);

    }


    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0){
                tvSecond.setText("secondNum : " + secondNum);
                Log.i(TAG, "secondNum in handler :" + secondNum);
            }
        }
    };



    class NewThread extends Thread{

        @Override
        public void run() {
            while (isThread){
                secondNum++;
                Log.i(TAG, "secondNum in thread :" + secondNum);

                try {
                    Thread.sleep(500);
                }catch (Exception e){
                    e.printStackTrace();
                }

                Message msg =new Message();
                msg=Message.obtain();
                msg.what=0;
                msg.arg1=0;
                msg.arg2=0;
                msg.obj=null;
                handler.sendMessage(msg);
                handler.sendEmptyMessage(0);
            }
        }
    }





}






 

5. runOnUiThread

 

 

 

 

 class NewThread extends Thread{
        @Override
        public void run() {

            while (isThread){
                secondNum++;

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                     tvSecond.setText("secondNum : " +secondNum);

                    }
                });

                try{
                    //0.5초
                    Thread.sleep(500);

                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i("ThreadOneActivity", " "+secondNum);

            }

        }
    }

 

class ThreadFiveActivity
package com.example.choi.my21;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ThreadFiveActivity extends AppCompatActivity {

    private int mainNum;
    private int secondNum;
    private TextView tvMain, tvSecond;
    private Button btnStart;

    private Button stopThread;

    private boolean isThread=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thread_five);


        tvMain=(TextView)findViewById(R.id.tv_main_thread);
        tvSecond=(TextView)findViewById(R.id.tv_second_thread);
        btnStart=(Button)findViewById(R.id.bt_start);
        stopThread=(Button)findViewById(R.id.stopThread);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=true;
                startNum();
            }
        });


        stopThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isThread=false;

                Toast.makeText(ThreadFiveActivity.this, "쓰레드 종료", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void startNum(){
        mainNum++;

        NewThread newThread=new NewThread();
        newThread.setDaemon(true);
        newThread.start();

        tvMain.setText("mainNum :" + mainNum);

    }


    class NewThread extends Thread{
        @Override
        public void run() {

            while (isThread){
                secondNum++;

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                     tvSecond.setText("secondNum : " +secondNum);

                    }
                });

                try{
                    //0.5초
                    Thread.sleep(500);

                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i("ThreadOneActivity", " "+secondNum);

            }

        }
    }







}

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

우리는 앞으로 굴러오는 것을 환영하자. 그러나 다른 것들은 갈망하지 말자. -앙드레지드

댓글 ( 4)

댓글 남기기

작성