如何等待所有线程完成执行? [英] How to wait until all threads complete their execution?

查看:142
本文介绍了如何等待所有线程完成执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在使用HTTP请求库来下载文件。这个库在里面使用线程。现在,我想在主线程上等待,直到其他线程完成它们的执行。

所有其他解决方案,我发现通过谷歌搜索只有工作,如果我有权访问线程库中使用的变量。但是这些都是我无法访问的。



以下是我目前使用的内容:

  package smartzero.eightnoteight.testfirebase; 

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;

import java.util.Scanner;

public class Main {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
System.out.print(email:);
String email = in.nextLine();
System.out.print(password:);
String password = in.nextLine();
Firebase fb =新的Firebase(https://nullform.firebaseio.com);
fb.authWithPassword(email,password,new AuthResultHandler());
尝试{
Thread.sleep(1000000);
} catch(InterruptedException e){
e.printStackTrace();
}
}

公共静态类AuthResultHandler实现Firebase.AuthResultHandler {
@覆盖
公共onAuthenticated(的authData的authData)无效{
系统.out.println(认证成功);
String uid = authData.getUid();
新的RunTests(uid);

$ b @Override
public void onAuthenticationError(FirebaseError firebaseError){
System.out.println(authentication failed。);


$ b $ / code $ / pre
$ b $ PS: Firebase使用firebase-client-jvm在我的电脑上。

解决方案

您应该使用Firebase提供的事件:

b
$ b'pre> fb.authWithPassword(电子邮件,密码,新AuthResultHandler(){
@覆盖
公共无效onAuthenticated(的authData的authData){
//做一些事情,如果认证成功

$ b @Override
public void onAuthenticationError(FirebaseError error){
//处理错误
}
});

你可以,如果你真的想在主要等待的话:

  void main(String [] args){
boolean finished = false;
fb.authWithPassword(电子邮件,密码,新AuthResultHandler(){
@覆盖
公共无效onAuthenticated(的authData的authData){
结束= TRUE;
}
});
while(!finished){
Thread.sleep(1);
}
}

更多的伪代码。它没有捕获中断的异常,并且如果有错误(onAuthenticationError)则永远阻塞。我也不会推荐这个。忙碌的等待几乎不是一个好主意。


Say I'm using a HTTP requests library for downloading files. This library uses threads inside. Now, I want to wait on the main thread until other threads complete their execution.

All the other solutions that I found by googling only work if I have access to the Thread variables that were used in the library. But these are not accessible to me.

Here's what i'm using currently:

package smartzero.eightnoteight.testfirebase;

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("email: ");
        String email = in.nextLine();
        System.out.print("password: ");
        String password = in.nextLine();
        Firebase fb = new Firebase("https://nullform.firebaseio.com");
        fb.authWithPassword(email, password, new AuthResultHandler());
        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class AuthResultHandler implements Firebase.AuthResultHandler {
        @Override
        public void onAuthenticated(AuthData authData) {
            System.out.println("authentication successful");
            String uid = authData.getUid();
            new RunTests(uid);
        }

        @Override
        public void onAuthenticationError(FirebaseError firebaseError) {
            System.out.println("authentication failed.");
        }
    }
}

PS: i'm testing firebase using firebase-client-jvm on my pc.

解决方案

You should use the events provided by Firebase:

fb.authWithPassword(email, password, new AuthResultHandler(){
    @Override
    public void onAuthenticated(AuthData authData) {
        //do something if authentication successful
    }

    @Override
    public void onAuthenticationError(FirebaseError error) {
        //handle error
    }
});

You could, if you really want to wait in the main do this:

void main(String[] args) {
    boolean finished = false;
    fb.authWithPassword(email, password, new AuthResultHandler(){
        @Override
        public void onAuthenticated(AuthData authData) {
            finished = true;
        }
    });
    while (!finished){
        Thread.sleep(1);
    }
}

Thats more of a pseudocode. It doesnt catch the interrupted exception and blocks forever if there is an error (onAuthenticationError). Also i would not recommend this. Busy waiting is almost never a good idea.

这篇关于如何等待所有线程完成执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆