마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

쓰레드 에 대해서 질문드립니다.

0 추천
public void timeThread(){

     new thread(new Runnable() {

            public void run(){

                    try{

                        Thread.sleep(5000);

                    } catch(Throwable ex) {

                          ex.printStackTrace();

                   }

                   statActivity(intent);

            }

       });.start();

}

 

 

이런식의 소스일때

5초후에 액티비티가 넘어가는데요...

궁금한 점은 액티비티가 넘어갈때 쓰레드가 죽는지 궁금합니다.

따로 쓰레드 죽이는 명령은 안 내렸거든요...

만약에 쓰레드 죽일려면 머라고 소스 작성해야하나요???

 

이런 종류의 쓰레드는 첨이라.

초보질문 드려서 죄송합니다.~
앤드류이드 (6,190 포인트) 님이 2014년 4월 9일 질문
이런식으로 하면 API 11 이상에서 MainThread에서 UI작업 안한다고 오류납니다.
개발에 참고하시면 좋을듯.

2개의 답변

+1 추천
 
채택된 답변
제 경우 Runnable 은 그렇게 바로 쓰고,

Thread의 경우 멤버 변수(필드)로 갖고 있다가 mThread.interrupt() 를 호출하여 종료 시킵니다.

그리고 쓰레드가 살았는지 확실히 확인하시려면 쓰레드 생성 시 쓰레드 이름까지 주는 생성자를 사용하시고

DDMS로 Thread 목록을 살펴보세요
Gioskhan (12,060 포인트) 님이 2014년 4월 9일 답변
앤드류이드님이 2014년 4월 10일 채택됨
+1 추천

스레드가 종료문제는  Gioskhan님 답변을 참고하시고

5초 딜레이 주려면 

new Handler().postDelayed(new Runnable(), 5000);

이렇게 사용하는게 좋습니다.

 

public final boolean postDelayed (Runnable r, long delayMillis)

Added in API level 1

Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.

Parameters
r The Runnable that will be executed.
delayMillis The delay (in milliseconds) until the Runnable will be executed.
Returns
  • Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.

 

q1212 (26,020 포인트) 님이 2014년 4월 10일 답변
...