안드로이드 개발 질문/답변
(글 수 45,052)
public class PracticeThreadActivity extends Activity {
int mMainValue = 0;
int mBackValue = 0;
TextView mMainText, mBackText;
BackRunnable runnable;
Thread thread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (TextView) findViewById(R.id.mMainTeaxt);
mBackText = (TextView) findViewById(R.id.mBackText);
runnable = new BackRunnable();
thread = new Thread(runnable);
thread.setDaemon(true);//메인스레드가 종료될 때, 같이종료시킴
thread.start();
}
public void mOnClick(View v){
Log.i("thread", "클릭하면들어옴");
Log.i("thread", "mMainValue 초기값" + mMainValue);
mMainValue++;
Log.i("thread", "mMainValue 증가된값" + mMainValue);
mMainText.setText("MainValue = " + mMainValue);
mBackText.setText("BackValue = " + mBackValue);
}
class BackRunnable implements Runnable{
@Override
public void run() {
Log.i("thread", "백그라운드로 들어옴");
while(true){
mBackValue++;
Log.i("thread", "mBackValue 값" + mBackValue);
try{
Thread.sleep(1000);
}catch(InterruptedException ie){;}
}
}
}
@Override
protected void onDestroy() {
if(thread.isDaemon()){
Log.i("thread", String.valueOf(thread.isDaemon()));
}
super.onDestroy();
}
}
위에것이 소스인데 책에서 setDaemon을 하면 다른 스레드가 없는이상 메인이 종료되면 스레드가 종료된다는데 로그를 찍어보니 계속 돌고있네요 에러가 궁금합니다. 도와주세요 펍님들




....종료하려면..그냥 트라이 캐치문에 파이널리 해주고 인터럽트 맥여주시고
디스트로이드 인터럽트 맥여주시면 되요...-_-;;