手动实现类似HandlerThread


public class LooperExecutor extends Thread implements Executor {
  private static final String TAG = "LooperExecutor";
  // Object used to signal that looper thread has started and Handler instance
  // associated with looper thread has been allocated.
  private final Object looperStartedEvent = new Object();
  private final List<Runnable> scheduledPeriodicRunnables = new LinkedList<Runnable>();
  private Handler handler = null;
  private boolean running = false;
  private long threadId;

  @Override
  public void run() {
    Looper.prepare();//为该线程准备好Looper对象 并且准备好消息队列
    synchronized (looperStartedEvent) {
      Log.d(TAG, "Looper thread started.");
      handler = new Handler();
      threadId = Thread.currentThread().getId();
      looperStartedEvent.notify();
    }
    Looper.loop();
  }

  public synchronized void requestStart() {
    if (running) {
      return;
    }
    running = true;
    handler = null;
    start();
    // Wait for Hander allocation.
    synchronized (looperStartedEvent) {
      while (handler == null) {
        try {
          looperStartedEvent.wait();//如果对象调用了wait方法就会使持有该对象的线程把该对象的控制权交出去,然后处于等待状态
          //调用任意对象的 wait() 方法导致该线程阻塞,该线程不可继续执行,并且该对象上的锁被释放
        } catch (InterruptedException e) {
          Log.e(TAG, "Can not start looper thread");
          running = false;
        }
      }
    }
  }

  public synchronized void requestStop() {
    if (!running) {
      return;
    }
    running = false;
    handler.post(new Runnable() {
      @Override
      public void run() {
        handler.getLooper().quit();
        Log.d(TAG, "Looper thread finished.");
      }
    });
  }

  // Checks if current thread is a looper thread.
  public boolean checkOnLooperThread() {
    return (Thread.currentThread().getId() == threadId);
  }

  public synchronized void scheduleAtFixedRate(final Runnable command, final long periodMillis) {
    if (!running) {
      Log.w(TAG, "Trying to schedule task for non running executor");
      return;
    }
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        if (running) {
          command.run();
          if (!handler.postDelayed(this, periodMillis)) {
            Log.e(TAG, "Failed to post a delayed runnable in the chain.");
          }
        }
      }
    };
    scheduledPeriodicRunnables.add(runnable);
    if (!handler.postDelayed(runnable, periodMillis)) {
      Log.e(TAG, "Failed to post a delayed runnable.");
    }
  }

  public synchronized void cancelScheduledTasks() {
    if (!running) {
      Log.w(TAG, "Trying to cancel schedule tasks for non running executor");
      return;
    }

    // Stop scheduled periodic tasks.
    for (Runnable r : scheduledPeriodicRunnables) {
      handler.removeCallbacks(r);
    }
    scheduledPeriodicRunnables.clear();
  }

  @Override
  public synchronized void execute(final Runnable runnable) {
    if (!running) {
      Log.w(TAG, "Running looper executor without calling requestStart()");
      return;
    }
    if (Thread.currentThread().getId() == threadId) {
      runnable.run();
    } else {
      handler.post(runnable);
    }
  }

  /**
   * Access to the handler for testing purposes.
   */
  Handler getHandler() {
    return handler;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值