更高效的循环暂停方法

4

如何改进代码以更好地与处理器配合工作?

我有一个类,在单独的线程中以固定周期执行某些任务。有时,这个过程可能会暂停和恢复。目前我正在使用一个标志来暂停,它运行良好,但在此方式下循环仍然会在进程暂停时加载处理器。是否有解决方法?

private boolean mIsCanceled = false;
private boolean mIsPaused = true; // TODO more efficient for processor way of pausing is required
private final Thread mTimerThread = new Thread(new Runnable() {
    @Override
    public void run() {
        while(!mIsCanceled){
            try {
                Thread.sleep(UPDATE_PERIOD);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (!mIsPaused){
                doStep();
            }
        }
    }
});

public MyClass(){
    mTimerThread.start();
}

private void pause(){
    mIsPaused = true;
}

private void resume(){
    mIsPaused = false;
}

private void doStep(){
    // Some code
}

请提供我代码的替代实现。
附注:环境是Android操作系统2.2+。

1
增加您的“UPDATE_PERIOD”将减少CPU负载。 - Sergey Kalinichenko
为什么你说“以这种方式循环,在进程暂停时仍会加载处理器”?Thread.sleep()应该在给定时间内放弃执行,因此在那段时间内不应该“加载处理器”。 - Rajesh J Advani
  1. 增加UPDATE_PERIOD对我没有作用。
  2. 我更喜欢我的任务等待课程暂停,而不是检查标志并执行循环(即使在循环中有睡眠)。
- Solvek
4个回答

6
可用的工具包括: wait/notify - 我们都试图摆脱这种古老的系统。 Semaphore - 一旦你的线程获取了它,你就需要一直持有它,直到释放。这意味着你不能在自己的线程中暂停。 CyclicBarrier - 必须每次使用时重新创建。 ReadWriteLock - 我最喜欢的。你可以让任意数量的线程暂停你,只有当所有线程都调用 resume 后,你才会恢复。你甚至可以自己暂停自己。
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * PauseableThread is a Thread with pause/resume and cancel methods.
 *
 * The meat of the process must implement `step`.
 *
 * You can either extend this and implement `step` or use the factory.
 *
 * Note that I cannot extend Thread because my resume will clash with Thread's deprecated one. 
 *
 * Usage: Either write a `Stepper` and run it in a `PausableThread` or extend `PausableThread` and call `blockIfPaused()` at appropriate points.
 */
public abstract class PauseableThread implements Runnable {
  // The lock.
  // We'll hold a read lock on it to pause the thread.
  // The thread will momentarily grab a write lock on it to pause.
  // This way you can have multiple pausers using normal locks.
  private final ReadWriteLock pause = new ReentrantReadWriteLock();
  // Flag to cancel the wholeprocess.
  private volatile boolean cancelled = false;
  // The exception that caused it to finish.
  private Exception thrown = null;

  @Override
  // The core run mechanism.
  public void run() {
    try {
      while (!cancelled) {
        // Block here if we're paused.
        blockIfPaused();
        // Do my work.
        step();
      }
    } catch (Exception ex) {
      // Just fall out when exception is thrown.
      thrown = ex;
    }
  }

  // Block if pause has been called without a matching resume.
  private void blockIfPaused() throws InterruptedException {
    try {
      // Grab a write lock. Will block if a read lock has been taken.
      pause.writeLock().lockInterruptibly();
    } finally {
      // Release the lock immediately to avoid blocking when pause is called.
      pause.writeLock().unlock();
    }

  }

  // Pause the work. NB: MUST be balanced by a resume.
  public void pause() {
    // We can wait for a lock here.
    pause.readLock().lock();
  }

  // Resume the work. NB: MUST be balanced by a pause.
  public void resume() {
    // Release the lock.
    pause.readLock().unlock();
  }

  // Stop.
  public void cancel() {
    // Stop everything.
    cancelled = true;
  }

  // start - like a thread.
  public void start() {
    // Wrap it in a thread.
    new Thread(this).start();
  }

  // Get the exceptuion that was thrown to stop the thread or null if the thread was cancelled.
  public Exception getThrown() {
    return thrown;
  }

  // Create this method to do stuff. 
  // Calls to this method will stop when pause is called.
  // Any thrown exception stops the whole process.
  public abstract void step() throws Exception;

  // Factory to wrap a Stepper in a PauseableThread
  public static PauseableThread make(Stepper stepper) {
    StepperThread pauseableStepper = new StepperThread(stepper);
    // That's the thread they can pause/resume.
    return pauseableStepper;
  }

  // One of these must be used.
  public interface Stepper {
    // A Stepper has a step method.
    // Any exception thrown causes the enclosing thread to stop.
    public void step() throws Exception;
  }

  // Holder for a Stepper.
  private static class StepperThread extends PauseableThread {
    private final Stepper stepper;

    StepperThread(Stepper stepper) {
      this.stepper = stepper;
    }

    @Override
    public void step() throws Exception {
      stepper.step();
    }
  }

  // My test counter.
  static int n = 0;

  // Test/demo.
  public static void main(String[] args) throws InterruptedException {

    try {
      // Simple stepper that just increments n.
      Stepper s = new Stepper() {
        @Override
        public void step() throws Exception {
          n += 1;
          Thread.sleep(10);
        }
      };
      PauseableThread t = PauseableThread.make(s);
      // Start it up.
      t.start();
      Thread.sleep(1000);
      t.pause();
      System.out.println("Paused: " + n);
      Thread.sleep(1000);
      System.out.println("Resuminng: " + n);
      t.resume();
      Thread.sleep(1000);
      t.cancel();
    } catch (Exception e) {
    }
  }
}

编辑:代码已经修改为更普遍的用途。


1

你最好的选择是使用wait()/notify()或者直接切换到ScheduledExecutorService

正确使用wait()/notify()可能会有些棘手。我强烈推荐阅读《Java并发编程实战》以了解更多关于线程的知识。


0

0

使用监视器而不是让线程休眠可以大大提高效率。您只需在代码中使用关键字synchronized创建块,以及充当监视器的最终对象。在API中查找更多有关监视器的信息。


似乎Android没有可用的监视器。 - Solvek
这个在Java API里面,如果你想了解的话,我建议阅读一下这个链接:http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html - Joelmob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接