我有一个在后台下载图片的线程,需要在上传开始前完成。我有一个按钮,开始上传,但不知道如何检查我的第一个线程是否完成/等待它完成。
这是我的下载主题:
t = new Thread(new Runnable() {
// NEW THREAD BECAUSE NETWORK REQUEST WILL BE MADE THAT WILL BE A LONG PROCESS & BLOCK UI
// IF CALLED IN UI THREAD
public void run() {
try {
for (int i = 0; i < Constants2photo.IMAGES.size(); i++) {
Uri myUri = Uri.parse(Constants2photo.IMAGES.get(i).get("url"));
String fileLocation = loadPicasaImageFromGallery(myUri);
Constants2photo.IMAGES.get(i).put("fileLocation", fileLocation);
System.out.println("fileloc: " + fileLocation);
}
System.out.println("done getting files - " + Constants2photo.IMAGES);
//this part would download the image to the media store//TODO add this to a background task so sending event is faster.
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
t.start();
threads.add(t); //this is a List
上面的代码就在我班级的onactivityresult方法中
带有线程#2的按钮代码需要在t完成后启动:
public void submitPhotos(View view){
//convert and submit photos here
Thread t2 = new Thread(new Runnable() {
// NEW THREAD BECAUSE NETWORK REQUEST WILL BE MADE THAT WILL BE A LONG PROCESS & BLOCK UI
// IF CALLED IN UI THREAD
public void run() {
try {
for (int i = 0; i < Constants2photo.IMAGES.size(); i++) {
...
System.out.println("encoded string: " + encodedString);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
t2.start();
那么我在哪里使用join才能在t完成时执行t2?
答案 0 :(得分:0)
Thread.join()是m0skit0提到的正确解决方案。忽略他/她关于不产生线程的评论。如果你想让UI响应其他目的,你只想在这个线程完成后激活上传按钮,那么join()就是你的解决方案。