我希望我的runnable在再次启动之前等待几秒钟,有关如何执行此操作的任何建议吗?
如果我使用this.wait();它不起作用,因为它停止了我的程序,我只想让runnable暂停。
答案 0 :(得分:0)
以下是2秒延迟final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
// Do stuff
handler.postDelayed(this, 2000);
}
};
handler.postDelayed(runnable, 2000);
var query = from movie in db.Movies where movie.Genres.Any(g => g.GenreID == 2) select movie;
答案 1 :(得分:0)
希望这有帮助
Handler handler = new Handler();
final Runnable runner = new Runnable()
{
public void run()
{
//Restarts
handler.postDelayed(this, 1000);
}
};
Thread thread = new Thread()
{
@Override
public void run() {
try {
while(true) {
sleep(1000);
handler.post(runner);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
答案 2 :(得分:0)
听起来主线程在您的示例中被阻止。请参阅下面的示例。这里主线程不会被调用wait阻止。
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
try {
synchronized (this) {
wait(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
看这里是一个线程的例子: Android.com / processes and threads