我在我的安卓应用服务中使用了Object.wait(timeout)。但它不包括在“深度睡眠模式”中花费的时间。我使用AlarmManager定期唤醒我的应用程序,所以从深度睡眠中醒来不是问题。问题是,wait(60000)不会在100秒钟的深度睡眠后终止。
当我在SystemClock帮助页面上阅读时,object.wait使用了uptimeMillis()方法,这种方法在深度睡眠中停止计数。为了满足我的需要,最好使用elapsedRealtime()。
如何实现Object.wait(timeout)的模拟,但使用elapsedRealtime方法?或者我能用什么代替?
我使用此方法的任务之一是生成"ping“数据包,以便在没有其他数据包排队一定时间的情况下通过网络发送。
发布于 2013-10-25 10:09:47
您已经提到(在注释中) interrupt()会导致线程的终止(终止),虽然这是完全错误的,但它只是向等待/连接/休眠线程抛出了一个异常。
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块,在此之后没有任何业务,所以这不是线程问题。
发布于 2013-10-28 03:29:22
我建议您不要使用普通的Object.wait()或Thread.sleep(),而是使用以下任何一种方法:
我认为1.是一种较好的方法。
如果您需要插入计时器对象或使用特定的或第三方的定时提供程序,则需要编写自己的计划程序来包装ScheduledExecutorService,然后使用自己的计时器转换时间或从自己的计时器中获取时间。基本上,您使用自己的时间计算在包装服务上启动一个预定任务。
我在我的参与者模型中有这样一个调度器的示例如下所示。看看这个包中的DefaultScheduler。它可能有点问题(我还没有对它进行充分的测试),但它应该给你一个好主意。
http://sourceforge.net/p/jalgo/code-0/HEAD/tree/trunk/src/org/as/algo/threading/
发布于 2013-10-30 09:19:58
不确定它是否做了您想做的事情,但是我写这篇文章是为了暂停一段时间,但是让其他线程过早地唤醒我。
它在内部使用BlockingQueue来实现它的睡眠,因此它避免使用sleep和wait以及它们带来的所有痛苦。
我不确定它在安卓系统下会如何运行,我不知道它是如何工作的,但我怀疑你现有的AlarmManager工作会适应。
/**
* 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();
}
}https://stackoverflow.com/questions/19494925
复制相似问题