我正在使用Java 1.5從命令行讀取多個參數。參數是平面文件的名稱。我循環通過main方法中的參數,並調用一個方法,這反過來又創建了一堆線程來處理文件。我需要暫停循環,直到所有線程處理第一個參數完成,然後繼續創建第二個參數的線程。我怎麼排隊的參數或暫停在我的主要方法循環執行,直到所有線程處理當前參數完成?在main方法中暫停執行循環,直到所有線程完成Java 1.5
2
A
回答
0
使用線程池和執行程序。看看java.util.concurrent
包。
for(String argument:args){
//you said you want multiple threads to work on a single argument.
//create callables instead and use a ThreadPool
List<Callable<YourResult>> lstCallables = createCallablesFor(argument);
List<Future<YourResult>> futures = Executors.newCachedThreadPool().invokeAll(lstCallables);
for(Future<YourResult> future:futures){
//this get() waits until the thread behind the current future is done.
// it also returns whatever your callable might return.
future.get();
}
// at this point, all the threads working on the current argument are finished
// and the next loop iteration works on the next argument
}
0
不知您是否在尋找類似cyclic barriers的東西。
-1
您需要在一個參數的循環內啓動線程作業,以便在一個作業完成後,下一個循環啓動並開始下一個參數的下一個線程作業。此外,你可以在你定義的線程工作中工作。
例子:這只是一個片段
for (int i = 0; i < count; i++) {
t[i] = new RunDemo();
String[] serverList = srv[i].split(",");
String logName = filename + "_" + serverList[0] + "_log";
String sql = "INSERT INTO .....(any query)";
t[i].setStr("sqlplus -L " + username[i] + "/" + password[i] + "@"
+ serverList[1] + ":" + serverList[2] + "/" + serverList[3]
+ " @" + filename1);
t[i].setLogName(logName);
t[i].setDirectory(dir);
try{
conn.UpdateQuery(sql);
log.info("Inserted into the table data with query " + sql);
}
catch (Exception e){
log.info("The data can't be inserted into table with " + e.getMessage() + " sql query " + sql);
}
new Thread(t[i]).start();
}
在這裏,在不同的服務器列表的每一個循環的新線程創建並啓動。
現在的工作定義如下:
public void run() {
JShell jshell = new JShell();
try {
log.info("Command is: " + this.str + " log name: " + this.LogName + " in directory: " + this.directory);
jshell.executeCommand(this.str, this.LogName, this.directory);
log.info("Executed command successfully");
} catch (Exception e1) {
log.info("Error at executing command with error stack: ");
e1.printStackTrace();
}
DBConnection conn1 = new DBConnection();
String sql = "UPDATE patcheventlog SET ENDTIME=SYSDATE WHERE LOGFILE='" + this.directory + this.LogName + "'";
try {
//conn1.callConnection("192.168.8.81", "d2he");
conn1.callConnection(ip, sid);
conn1.UpdateQuery(sql);
conn1.disposeConnection();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.print(this.LogName);
}
因此,這是你如何與循環內的線程工作。你不需要暫停你的循環。
希望有所幫助。
相關問題
- 1. 暫停函數執行流程直到ajax請求完成
- 2. 如何停止執行FOR循環,直到代碼完成
- 3. 禁用所有控件,直到線程完成執行
- 4. Java在循環中暫停
- 5. 不要退出方法,直到所有線程完成
- 6. 暫停執行,直到批處理文件完成
- 7. 如何阻止主線程,直到所有其他線程完成執行?
- 8. Python暫停線程執行
- 9. 停止執行,直到完成上述方法?
- 10. 停止方法直到另一個執行完成?
- 11. Linux - 暫停循環,直到按下鍵
- 12. 暫停rootEpic直到持續完成
- 13. 停止線程直到其他完成
- 14. 有什麼辦法暫停for循環,直到變量爲真
- 15. 每次執行後暫停循環
- 16. 不會暫停執行的C++循環
- 17. 使用循環屏障不會等到所有線程完成
- 18. 在運行方法中沒有循環的Java線程
- 19. 程序暫停在循環
- 20. 在循環中創建線程並等待所有線程完成/中止
- 21. Java暫停執行
- 22. java工作線程中的無限循環違反ScheduledExecutorService暫停
- 23. 如何使方法暫停執行,直到先前使用異步回調調用的方法完成
- 24. 暫停循環並暫停
- 25. 線程暫停執行Tkinter程序
- 26. 暫停執行,直到Websocket連接
- 27. 暫停TimerTask直到下一次執行
- 28. 如何在Java中的線程中停止執行該方法?
- 29. 在運行時暫停當前進程中的所有線程
- 30. python itizip循環遍歷所有iterables,直到最長完成
主題和加入 –