[英]How to make multiple Threads run at the same time using a for loop in Java?
[英]How to run multiple logins at a time with using for loop?
我正在处理k-9邮件。 所有设置步骤均已完美完成,但是根据我的要求,我希望使用多个凭据打开多个帐户。 我尝试使用不同线程的for
循环,但仅执行最后一次登录!
我尝试这样,但它不起作用。
任何人都可以提出建议...
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();
}
请尝试以下操作,并告诉我。
注意:我还没有完全测试它。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.