每次单击按钮时都会调用此代码
//Thread called when click a button
Thread a = new Thread(new Runnable() {
@Override
public void run() {
synchronized ((Object) contadordeMierda){
Random rn = new Random();
int n= rn.nextInt(10) + 1;
contador++;
try {
Thread.sleep(n*100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(contador);
}
}
});
a.start();
当我快速触摸它几次时,我得到这个输出:
I/System.out:1
I/System.out:2
I/System.out:5
I/System.out:5
I/System.out:9
I/System.out:9
I/System.out:9
I/System.out:9
I/System.out:9
...
如何等待一个线程完成才能启动另一个线程?所以打印结果是 1 2 3 4 5 6 7 8 9 10 11 12...?
最佳答案
这就是 join() function 的确切目的。多次被问到的问题:How to wait for a number of threads to complete? .
关于java - Android中如何等待一个线程启动另一个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28155367/