android 双线程等待,在Java/Android中启动另一个线程之前如何等待线程完成?
生活随笔
收集整理的這篇文章主要介紹了
android 双线程等待,在Java/Android中启动另一个线程之前如何等待线程完成?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在回答您的問題之前,我強烈建議您查看
ExecutorServices,例如
ThreadPoolExecutor。
現在回答你的問題:
如果要等待上一個線程完成,在開始下一步之前,您可以在之間添加thread.join():
for(int i = 0; i < 10; i++) {
thread = new Thread(this);
thread.start();
thread.join(); // Wait for it to finish.
}
如果你想啟動10個線程,讓他們做他們的工作,然后繼續(xù),你在循環(huán)后加入他們:
Thread[] threads = new Thread[10];
for(int i = 0; i < threads.length; i++) {
threads[i] = new Thread(this);
threads[i].start();
}
// Wait for all of the threads to finish.
for (Thread thread : threads)
thread.join();
總結
以上是生活随笔為你收集整理的android 双线程等待,在Java/Android中启动另一个线程之前如何等待线程完成?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线程run方法和start方法的区别
- 下一篇: c语言Wndproc未定义,为什么我的老