您好,这不是我的主要代码,但它足以显示我的问题。
我需要创建 UI 延迟(两次) - 一个接一个 我无法单独使用 handler.postDelay 来完成它。
所以我尝试用线程来制作它。
我想做的是让 t2 仅在 t1 结束后开始。
谁能帮帮我吗?
final Handler handler1 = new Handler();
final Handler handler2 = new Handler();
synchronized(this)
{
Thread t1= new Thread(new Runnable() {
public void run()
{
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.RED);
notifyAll();
}
}, 3000);
}
});
Thread t2= new Thread(new Runnable() {
public void run()
{
handler2.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.YELLOW);
}
}, 3000);
}
});
t1.start();
while (t1.isAlive() )
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
t2.start();
}
最佳答案
保持简单:
- 一个处理程序,无线程
- 发布一个延迟的可运行程序,做两件事:
- 设置红色背景
- 发布另一个延迟的可运行文件(设置黄色背景)
final Handler handler1 = new Handler(Looper.getMainLooper());
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.RED);
//post another action to be executed in 3 sec
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.YELLOW);
}
}, 3000);
}
}, 3000);
关于java - Android Java Handler,线程同步块(synchronized block)(notifyall和Wait),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26193079/