首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用for循环一次运行多个登录?

如何使用for循环一次运行多个登录?
EN

Stack Overflow用户
提问于 2019-03-11 20:26:42
回答 1查看 64关注 0票数 0

我在处理k-9邮件。所有的设置步骤都完美地完成了,但根据我的要求,我希望多个帐户打开与多个凭证存在。我尝试使用不同线程的for循环,但它只执行了最后一次登录!

我试着这样做,但它不起作用。

任何人请给我建议...

代码语言:javascript
复制
gmail_creds= Helpers.getArrayList(WelcomeMessage.this,"email_creds");
   for( int i=0;i<gmail_creds.size();i++){
final  int j=i;
                  //k

       new Thread(new Runnable() {

                public void run(){

                    Helpers.saveStringInSP(WelcomeMessage.this,"userEmail", gmail_creds.get(j).userEmail);
                    Helpers.saveStringInSP(WelcomeMessage.this,"userPassword",gmail_creds.get(j).userPassword);

                    AccountSetupBasics.actionNewAccount(WelcomeMessage.this);


                 }
            }).start();




        }
EN

回答 1

Stack Overflow用户

发布于 2019-03-11 21:05:17

尝试以下操作,并让我知道。

注意:我还没有完全测试它。

代码语言:javascript
复制
gmail_creds= Helpers.getArrayList(WelcomeMessage.this,"email_creds");
List<Thread> threads = new ArrayList<Thread>();
for( int i = 0; i < gmail_creds.size(); i++){
    final int j = i;
    Thread t = new Thread(new Runnable() {
        public void run(){
            Helpers.saveStringInSP(WelcomeMessage.this,"userEmail", gmail_creds.get(j).userEmail);
            Helpers.saveStringInSP(WelcomeMessage.this,"userPassword",gmail_creds.get(j).userPassword);
            AccountSetupBasics.actionNewAccount(WelcomeMessage.this);
        }
    })
    t.start();
    threads.add(t);
}

// Let all threads to finish execution prior continuing main thread.
try {
    for(Threat t: threads){
        t.join();
    }
} catch(InterruptedException ie){
    ie.printStackTrace();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55101838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档