![](/img/trans.png)
[英]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.