您的位置:首页 > 移动开发 > Android开发

Android中的java层的线程暂停和恢复实现

2013-06-18 11:12 281 查看
/**

 * 基础线程对象.

 *

 * @author jevan

 * @version (1.0 at 2013-6-17)

 * @version (1.1 at 2013-7-2) 增加onDestory接口{@link #onDestory()},增加stop方法{@link #stop() }。

 */

public
abstract
class BaseThread implements Runnable {

   public
static
final
int SUSPEND_TIME_MILLISECONDS = 50;

 
   private String name;

   private Thread mThread;

 
   private
boolean suspendFlag = false;// 控制线程的执行

   // private int i = 0;

   private String TAG = getName();

 
   /**

    * 构造函数

    * @param name 线程名称。

    * @param suspend 初始化是否暂停。

    */

   public BaseThread(String name, boolean suspend) {

      suspendFlag = suspend;

      this.name = name;

      mThread = new Thread(this, name);

      System.out.println("new Thread: " + mThread);

      mThread.start();

   }

 
   public
void run() {

      try {

         while (true) {

            // System.out.println(name + ": " + i++);

            synchronized (this) {

               while (suspendFlag) {

                  wait();

               }

            }

            Thread.sleep(SUSPEND_TIME_MILLISECONDS);

            process();

         }

      } catch (InterruptedException e) {

         e.printStackTrace();

         onDestory();

      }

      Log.i(TAG, name + " exited");

   }

 
   /**

    * 线程处理接口。

    */

   public
abstract
void process();

 
   /**

    * 线程暂停

    */

   public
void suspend() {

      this.suspendFlag = true;

   }

 
   /**

    * 唤醒线程

    */

   public
synchronized
void resume() {

      this.suspendFlag = false;

      notify();

   }

 
   /**

    * 返回线程名

    *

    * @return name

    */

   public String getName() {

      return name;

   }

 
   /**

    * 获取线程对象。

    *

    * @return 线程对象。

    */

   public Thread getT() {

      return mThread;

   }

 
   /**

    * 停止线程运行。

    */

   public
void stop() {

      if (mThread != null){

 
         mThread.interrupt();

         mThread = null;

      }

   }

 
   /**

    * 线程处理接口。

    */

   public
void onDestory()

   {

      Log.i(TAG, name + " destory!");

   }

 
}

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: