首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >elapsedRealtime模拟Object.wait

elapsedRealtime模拟Object.wait
EN

Stack Overflow用户
提问于 2013-10-21 12:46:33
回答 3查看 361关注 0票数 3

我在我的安卓应用服务中使用了Object.wait(timeout)。但它不包括在“深度睡眠模式”中花费的时间。我使用AlarmManager定期唤醒我的应用程序,所以从深度睡眠中醒来不是问题。问题是,wait(60000)不会在100秒钟的深度睡眠后终止。

当我在SystemClock帮助页面上阅读时,object.wait使用了uptimeMillis()方法,这种方法在深度睡眠中停止计数。为了满足我的需要,最好使用elapsedRealtime()

如何实现Object.wait(timeout)的模拟,但使用elapsedRealtime方法?或者我能用什么代替?

我使用此方法的任务之一是生成"ping“数据包,以便在没有其他数据包排队一定时间的情况下通过网络发送。

EN

回答 3

Stack Overflow用户

发布于 2013-10-25 10:09:47

您已经提到(在注释中) interrupt()会导致线程的终止(终止),虽然这是完全错误的,但它只是向等待/连接/休眠线程抛出了一个异常。

代码语言:javascript
复制
public void Foo implements Runnable{
  public void run(){
   //do some work
   try{Thread.sleep(10000);}catch(Exception ex){/*when thread got interrupted*/}
   //do something else
  }
}

问题就在这里,因为您将所有的业务都放在一个try块中,所以中断会导致代码跳入catch块,在此之后没有任何业务,所以这不是线程问题。

票数 1
EN

Stack Overflow用户

发布于 2013-10-28 03:29:22

我建议您不要使用普通的Object.wait()或Thread.sleep(),而是使用以下任何一种方法:

  1. 使用java.util.concurrent.newScheduledThreadPool,它使您能够安排固定间隔或延迟的任务。使用threadCount =1初始化线程池会给出一个线程。
  2. 使用java.util.Timer,它允许您调度TimerTask。

我认为1.是一种较好的方法。

如果您需要插入计时器对象或使用特定的或第三方的定时提供程序,则需要编写自己的计划程序来包装ScheduledExecutorService,然后使用自己的计时器转换时间或从自己的计时器中获取时间。基本上,您使用自己的时间计算在包装服务上启动一个预定任务。

我在我的参与者模型中有这样一个调度器的示例如下所示。看看这个包中的DefaultScheduler。它可能有点问题(我还没有对它进行充分的测试),但它应该给你一个好主意。

http://sourceforge.net/p/jalgo/code-0/HEAD/tree/trunk/src/org/as/algo/threading/

票数 1
EN

Stack Overflow用户

发布于 2013-10-30 09:19:58

不确定它是否做了您想做的事情,但是我写这篇文章是为了暂停一段时间,但是让其他线程过早地唤醒我。

它在内部使用BlockingQueue来实现它的睡眠,因此它避免使用sleepwait以及它们带来的所有痛苦。

我不确定它在安卓系统下会如何运行,我不知道它是如何工作的,但我怀疑你现有的AlarmManager工作会适应。

代码语言:javascript
复制
/**
 * Use one of these to doze for a certain time.
 *
 * The dozing is fully interruptable.
 *
 * Another thread can stop the caller's doze with either a wakeup call or an abort call.
 *
 * These can be interpreted in any way you like but it is intended that a Wakeup is
 * interpreted as a normal awakening and should probably be treated in exactly the
 * same way as an Alarm. An Abort should probably be interpreted as a suggestion
 * to abandon the process.
 */
public class Doze {
  // Special alarm messages.
  public enum Alarm {
    // Standard timeout.
    Alarm,
    // Forced wake from your doze.
    Wakeup,
    // Abort the whole Doze process.
    Abort;
  }
  // My queue to wait on.
  private final BlockingQueue<Alarm> doze = new ArrayBlockingQueue<>(1);
  // How long to wait by default.
  private final long wait;

  public Doze(long wait) {
    this.wait = wait;
  }

  public Doze() {
    this(0);
  }

  public Alarm doze() throws InterruptedException {
    // Wait that long.
    return doze(wait);
  }

  public Alarm doze(long wait) throws InterruptedException {
    // Wait that long.
    Alarm poll = doze.poll(wait, TimeUnit.MILLISECONDS);
    // If we got nothing then it must be a normal wakeup.
    return poll == null ? Alarm.Alarm : poll;
  }

  public void wakeup() {
    // Just post a Wakeup.
    doze.add(Alarm.Wakeup);
  }

  public void abort() {
    // Signal the system to abort.
    doze.add(Alarm.Abort);
  }

  private static long elapsed ( long start ) {
    return System.currentTimeMillis() - start;
  }

  // Test code.
  public static void main(String[] args) throws InterruptedException {
    // Doze for 1 second at a time.
    final Doze d = new Doze(1 * 1000);
    final long start = System.currentTimeMillis();

    // Start a dozing thread.
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Alarm a = d.doze();
          // Wait forever until we are aborted.
          while (a != Alarm.Abort) {
            System.out.println(elapsed(start) + ": Doze returned " + a);
            a = d.doze();
          }
          System.out.println(elapsed(start) + ": Doze returned " + a);
        } catch (InterruptedException ex) {
          // Just exit on interrupt.
        }
      }

    }).start();


    // Wait for a few seconds.
    Thread.sleep(3210);

    // Wake it up.
    d.wakeup();

    // Wait for a few seconds.
    Thread.sleep(4321);

    // Abort it.
    d.abort();


  }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19494925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档