添加到 运行 线程或等待线程完成后再执行下一个代码
Add to a running thread or wait for thread to finished before executing next code
我有一个线程正在后台下载图像,需要它在上传开始前完成。我有一个开始上传的按钮,但不确定如何检查我的第一个线程是否完成/等待它完成。
这是我的下载线程:
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
以上代码只是在我的class
中的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 使 t2 仅在 t 完成时才执行?
Thread.join() 是 m0skit0 提到的正确解决方案。忽略 his/her 关于不生成线程的评论。如果您想让 UI 保持对其他目的的响应,并且只想在此线程完成后激活上传按钮,那么 join() 是您的解决方案。
我有一个线程正在后台下载图像,需要它在上传开始前完成。我有一个开始上传的按钮,但不确定如何检查我的第一个线程是否完成/等待它完成。
这是我的下载线程:
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
以上代码只是在我的class
中的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 使 t2 仅在 t 完成时才执行?
Thread.join() 是 m0skit0 提到的正确解决方案。忽略 his/her 关于不生成线程的评论。如果您想让 UI 保持对其他目的的响应,并且只想在此线程完成后激活上传按钮,那么 join() 是您的解决方案。