我正在使用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. 暂停的foreach循环,直到第一次迭代完成
- 5. Java在循环中暂停
- 6. C#等待所有线程完成Main()
- 7. 不要退出方法,直到所有线程完成
- 8. Javascript/jQuery - 如何暂停执行循环,直到提交表单?
- 9. 暂停执行,直到批处理文件完成
- 10. 如何阻止主线程,直到所有其他线程完成执行?
- 11. 暂停的std ::线程,直到功能完成
- 12. pthread - 暂停/暂停所有线程
- 13. Python暂停线程执行
- 14. 停止方法直到另一个执行完成?
- 15. 停止执行,直到完成上述方法?
- 16. Java暂停程序执行
- 17. Linux - 暂停循环,直到按下键
- 18. 暂停rootEpic直到持续完成
- 19. 暂停新的BackGroundWorker,直到完成前
- 20. 暂停mouseup/click直到$ .get完成
- 21. 停止线程直到其他完成
- 22. Java while while循环没有暂停当前线程
- 23. 暂停主线程的执行,直到NSWindow关闭
- 24. 有什么办法暂停for循环,直到变量为真
- 25. 使用循环屏障不会等到所有线程完成
- 26. 在运行方法中没有循环的Java线程
- 27. 不会暂停执行的C++循环
- 28. 每次执行后暂停循环
- 29. 程序暂停在循环
- 30. 在循环中创建线程并等待所有线程完成/中止
主题和加入 –