我正在编写需要使用线程的Android应用程序 在这个应用程序中,我有3个线程,从应用程序的开头开始 这是我的代码:
Thread t1,t2,t3;
protected void onCreate(Bundle savedInstanceState)
{
t1=new Thread(new Runnable()
{
public void run()
{
//do some long stuffs
t3.notify();
}
});
t2=new Thread(new Runnable()
{
public void run()
{
//do some long stuffs
t3.notify();
}
});
t3=new Thread(new Runnable()
{
public void run()
{
this.wait();
//run after t1 & t2 is done.
}
});
t1.start();
t2.start();
t3.start();
}
现在我想睡眠 t3 线程并在t1和t2完成工作时运行它。
答案 0 :(得分:1)
将t1,t2引用传递给t3并在t3.run()内部等待其他线程:
t1.join();
t2.join();
像这样:
final Thread t1, t2, t3;
t1 = new Thread(new Runnable() {
public void run() {
//do some long stuffs
}
});
t2 = new Thread(new Runnable() {
public void run() {
//do some long stuffs
}
});
t3 = new Thread(new Runnable() {
public void run() {
try {
t1.join();
t2.join();
// t1 & t2 have finished
// continue with t3 stuff
} catch (InterruptedException e) {
//handle it
}
}
});
t1.start();
t2.start();
t3.start();