如何使计时器任务要等到runOnUiThread完成 [英] How to make timer task to wait till runOnUiThread completed
本文介绍了如何使计时器任务要等到runOnUiThread完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一定的时间后更新的用户界面,对此我有创建一个定时器时间表,里面我打电话的runOnUiThread。
timer.scheduleAtFixedRate(新的TimerTask(){ 公共无效的run(){
的System.out.println(1);
尝试{ 的System.out.println(2);
的System.out.println(3); runOnUiThread(新的Runnable(){ 公共无效的run(){
的System.out.println(4);
的System.out.println(5);
的System.out.println(6);
的System.out.println(7);
}
}); 的System.out.println(8);
}赶上(例外五){ e.printStackTrace();
} } },延迟期);
的System.out.println(9);
我有问题,达成后,3,计时器线程跳转到8,并且在UI线程从4执行后。
我想使计时器线程等到UI线程7才应该移动到8完成其工作。
样本输出
01-05 00:30:16.308:我/的System.out(1394):1
01-05 00:30:16.308:我/的System.out(1394):2
01-05 00:30:16.308:我/的System.out(1394):3
01-05 00:30:16.308:我/的System.out(1394):8
01-05 00:30:16.308:我/的System.out(1394):4
01-05 00:30:16.308:我/的System.out(1394):5
01-05 00:30:16.308:我/的System.out(1394):6
01-05 00:30:16.308:我/的System.out(1394):7
01-05 00:30:17.307:我/的System.out(1394):1
01-05 00:30:17.307:我/的System.out(1394):2
01-05 00:30:17.307:我/的System.out(1394):3
01-05 00:30:17.307:我/的System.out(1394):8
01-05 00:30:17.307:我/的System.out(1394):4
01-05 00:30:17.323:我/的System.out(1394):5
01-05 00:30:17.323:我/的System.out(1394):6
01-05 00:30:17.323:我/的System.out(1394):7
解决方案
试试这个
对象锁=新的对象();
timer.scheduleAtFixedRate(新的TimerTask(){ 公共无效的run(){
的System.out.println(1);
尝试{ 的System.out.println(2);
的System.out.println(3); runOnUiThread(新的Runnable(){ 公共无效的run(){
的System.out.println(4);
的System.out.println(5);
的System.out.println(6);
的System.out.println(7);
同步(锁定){lock.notify();}
}
});
尝试{
同步(锁定){lock.wait();}
}赶上(InterruptedException的X){}
的System.out.println(8);
}赶上(例外五){ e.printStackTrace();
} } },延迟期);
的System.out.println(9);
I need to update UI after certain time period, for which I have create a timer schedule and inside it I am calling the runOnUiThread.
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("1");
try {
System.out.println("2");
System.out.println("3");
runOnUiThread(new Runnable() {
public void run() {
System.out.println("4");
System.out.println("5");
System.out.println("6");
System.out.println("7");
}
});
System.out.println("8");
} catch (Exception e) {
e.printStackTrace();
}
}
}, delay, period);
System.out.println("9");
I am having the problem that after reaching "3", timer thread jumps to "8" and after that the UI thread runs from "4". I want to make timer thread to wait till UI thread completes its work at "7" only then it should move to "8".
Sample Output
01-05 00:30:16.308: I/System.out(1394): 1
01-05 00:30:16.308: I/System.out(1394): 2
01-05 00:30:16.308: I/System.out(1394): 3
01-05 00:30:16.308: I/System.out(1394): 8
01-05 00:30:16.308: I/System.out(1394): 4
01-05 00:30:16.308: I/System.out(1394): 5
01-05 00:30:16.308: I/System.out(1394): 6
01-05 00:30:16.308: I/System.out(1394): 7
01-05 00:30:17.307: I/System.out(1394): 1
01-05 00:30:17.307: I/System.out(1394): 2
01-05 00:30:17.307: I/System.out(1394): 3
01-05 00:30:17.307: I/System.out(1394): 8
01-05 00:30:17.307: I/System.out(1394): 4
01-05 00:30:17.323: I/System.out(1394): 5
01-05 00:30:17.323: I/System.out(1394): 6
01-05 00:30:17.323: I/System.out(1394): 7
解决方案
Try this
Object lock=new Object();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("1");
try {
System.out.println("2");
System.out.println("3");
runOnUiThread(new Runnable() {
public void run() {
System.out.println("4");
System.out.println("5");
System.out.println("6");
System.out.println("7");
synchronized(lock){lock.notify();}
}
});
try{
synchronized(lock){lock.wait();}
}catch(InterruptedException x){}
System.out.println("8");
} catch (Exception e) {
e.printStackTrace();
}
}
}, delay, period);
System.out.println("9");
这篇关于如何使计时器任务要等到runOnUiThread完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文