Lecture Notes/Android Basics

Java/Android Thread 구현 방법

kingjung 2012. 3. 21. 13:09

1. Thread 클래스 사용
 public class ThreadTest extends Activity {
    int mMainValue = 0;
    int mBackValue = 0;
   TextView mMainText;
   TextView mBackText;

   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.thread_threadtest);

      mMainText = (TextView)findViewById(R.id.mainvalue);
      mBackText = (TextView)findViewById(R.id.backvalue);
      Button btn = (Button)findViewById(R.id.increase);
      btn.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               mMainValue++;
               mMainText.setText("MainValue : " + mMainValue);
              mBackText.setText("BackValue : " + mBackValue);
          }
     });
  
     BackThread thread = new BackThread();
     thread.setDaemon(true);
     thread.start();
 }
 
 class BackThread extends Thread {
     public void run() {
        while (true) {
              mBackValue++;
              //mBackText.setText("BackValue : " + mBackValue);
              try {
                 Thread.sleep(1000);
              } catch (InterruptedException e) { }
        }
     } 

  } // end of main
 } // end of class

2. Runnable 인터페이스 사용
 public class ThreadTest extends Activity {
      int mMainValue = 0;
      int mBackValue = 0;
      TextView mMainText;
      TextView mBackText;

      public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.thread_threadtest);

           mMainText = (TextView)findViewById(R.id.mainvalue);
           mBackText = (TextView)findViewById(R.id.backvalue);
           Button btn = (Button)findViewById(R.id.increase);
           btn.setOnClickListener(new Button.OnClickListener() {
                 public void onClick(View v) {
                      mMainValue++;
                      mMainText.setText("MainValue : " + mMainValue);
                      mBackText.setText("BackValue : " + mBackValue);
                 }
           });
  
           BackRunnable runnable = new BackRunnable();
           Thread thread = new Thread(runnable);
            thread.setDaemon(true);
            thread.start();
       }
 
      class BackRunnable implements Runnable {
            public void run() {
                  while (true) {
                          mBackValue++;
                          try {
                               Thread.sleep(1000);
                          } catch (InterruptedException e) { }
                  }
            }
       }
 }