'thread'에 해당되는 글 2건

  1. 2014.12.05 Thread interrupt
  2. 2014.10.20 Message, Handler, obtain, Thread
자바 Java2014. 12. 5. 00:04






















public class JavaExam {

public static void main(String[] args) {

System.out.println("Start");

Thread threadA = new MyThread("Thread AAA");

threadA.start();


try {

Thread.sleep(1500);

} catch (InterruptedException e) {

e.printStackTrace();

}

threadA.interrupt();


System.out.println("------  끝  ------");

}

}


class MyThread extends Thread {

MyThread(String name) {

setName(name);

}


public void run() {


try {

Thread.sleep(10000);

}

catch (InterruptedException e) {

System.out.println("   " +getName() + "InterruptedException예외 발생");

}

System.out.println("   " +getName() +"   끝");

}

}


*************************************************


class JavaExam {

public static void main(String[] args) {

        System.out.println("------- 시작 -------");

        Thread threadA = new MyThread("Thread AAA");

        threadA.start();

        

        try {

Thread.sleep(1500);

} catch (InterruptedException e) {

e.printStackTrace();

}

        

        threadA.interrupt();

        try {

threadA.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

        

        System.out.println("------  끝  ------");

   

   }


}


class MyThread extends Thread {

MyThread(String name) {

setName(name);

}

    public void run() {

        

        try {

            Thread.sleep(10000);

        }

        catch (InterruptedException e) {

            System.out.println("   " +getName() + "InterruptedException예외 발생");

        }

        

        try {

Thread.sleep(10); // main 스레드 출력 순서 테스트

} catch (InterruptedException e) {

e.printStackTrace();

}

        System.out.println("    " +getName() +"   끝");

    }

}


**************************************************


class JavaExam {

public static void main(String[] args) {

        System.out.println("------- 시작 -------");

        MyThread threadA = new MyThread("Thread AAA");

        

        threadA.setRunning(true);

        threadA.start();

        

        try {

Thread.sleep(1500);

} catch (InterruptedException e) {

e.printStackTrace();

}

        

        threadA.interrupt();

        try {

threadA.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

        

boolean done = true;

threadA.setRunning(false);

while (done) {

try {

threadA.join();

done = false;

} catch (InterruptedException e) {

e.printStackTrace();

}

}

        

        System.out.println("------  끝  ------");

   }

}


class MyThread extends Thread {

boolean isRun;

MyThread(String name) {

setName(name);

}

public void setRunning(boolean run) {

this.isRun = run;

}

    public void run() {

        

        try {

            Thread.sleep(10000);

        }

        catch (InterruptedException e) {

            System.out.println("   " +getName() + "InterruptedException예외 발생");

        }

        

        try {

Thread.sleep(10); // main 스레드 출력 순서 테스트

} catch (InterruptedException e) {

e.printStackTrace();

}

        System.out.println("   "  +getName() +"   끝");

    }

}

Posted by 코드버무려
Android 안드로이드2014. 10. 20. 22:03


출처: 슈퍼드로이드


16. Activity에 대해서 - Thread 와 Android Main Thread

http://cafe.daum.net/superdroid/aAfL/98


--------------------------------------------------------------


package com.test.ThreadTest;


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 A extends Activity {

static final int REQUEST_DRAW_COUNT = 1;

TextView   mTextView = null;

Handler mHandler = null;

int mCount = 0;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        mTextView = (TextView) findViewById(R.id.text_view);

        

        //mHandler = new Handler(); // runnable 방식

        

        mHandler = new Handler() // handleMessage 방식

        {

        public void handleMessage(Message msg) {

        switch (msg.what)

        {

        case REQUEST_DRAW_COUNT:

        mCount = msg.arg1;

        mTextView.setText("Count : " + mCount);

        break;

        }

        }

        };

    }

    

    public void onClick(View v)

    { 

    switch(v.getId())

    {

    case R.id.set_count_btn:

    new Thread()

    {

    public void run()

    {

    /*  // runnable방식

    Runnable runnable = new Runnable ()

    {

    public void run()

    {

    mTextView.setText("count : " + mCount);

    }

    };

    Message msg = Message.obtain(mHandler, runnable);

    mCount = 50; 

    mHandler.sendMessage(msg); */

   

    // handleMessage 방식

    Message msg = Message.obtain(mHandler, REQUEST_DRAW_COUNT);

    msg.arg1 = 50;

    mHandler.sendMessage(msg);

    }  

    }.start();

   

    /*    //오류

    new Thread()

    {

    @Override

        public void run() 

        {

    mTextView.setText("Count : " + 50);

        }

   

    }.start(); */

   

    break;

    }

    }

}




Posted by 코드버무려