这样我生成线程,但在for循环之后如何检查所有线程是否存活
进一步的过程取决于所有的线程
如果线程为dead
,则进行进一步处理
for (int i = 0;i<List.size();i++) {
if(List.get(i).trim().length() >0 ){
Thread thread = new Thread("Thread"+i){
public void run(){
threading(List.get(i),ctx,userSesionObj);
}
};
thread.start();
}
}
这里threading
是我的函数,它执行我传递的不同数据
答案 0 :(得分:4)
您可以使用ExecutorService。事实上,我强烈推荐这一点。您可以指定不同的线程行为等。执行程序将为每个提交的线程返回一个Future对象,然后您可以遍历每个Future
收集结果。以下是:
for (Future f : futures) {
f.get(); // get a result here...
}
当所有Futures
都返回数据时(即线程完成时),将完成。 Executor API的工作级别高于join()
/ notify()
等,使用起来更安全。
有很多功能可以在这里使用,我会推荐tutorials。
答案 1 :(得分:2)
您可能希望join
线程。参看http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html。鉴于您可以暂停主线程,即。
答案 2 :(得分:1)
使用ExecutorService
- Executors
管理Thread
个对象和Async
任务井
- 这里不是直接运行线程的客户端,而是中间对象。
- 将此与submit()
方法结合使用,该方法返回Future
对象。
- Future
是异步任务的结果。
- 您可以使用Future的get()
方法,其具有阻止性质,或get()
方法以及isDone()
方法方法。
答案 3 :(得分:0)
答案 4 :(得分:0)
正如彼得所说,这应该做......
int count = selectedCategoryList.size();
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < count; i++) {
if(selectedCategoryList.get(i).trim().length() >0 ){
final Long CategoryID = Long.parseLong(selectedCategoryList.get(i));
Thread thread = new Thread("ReplaceMedicationThread"+i){
public void run(){
threadingForReplacingMedication(CategoryID,ctx,userSesionObj);
}
};
threads.add(thread):
}
}
//start them in a loop
//join them in a loop
//move on....
答案 5 :(得分:0)
在继续处理之前等待一些线程完成的简单方法是通过CompletionService。这就是你如何使用它:
// list of data that need to be processed before continuing execution
List<DataForProcessing> workList = // ...
// An instance of an Executor http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html
Executor exec = // ...
// asume that ResultData is a data structure returned from the processing each
// DataForProcessing instance. If processing does not return a value you could
// parameterise the completion service as CompletionService<Void>.
CompletionService<ResultData> service = new ExecutorCompletionService<ResultData>(executor);
// Submit the tasks for processing. Each task will be processed by the executor and
// its result will be wrapped in a Future and place in a BlockingQueue maintained by
// the CompletionService instance. Again, if your submitted task does not return
// a value you should parameterise your task as Callable<Void>.
for (DataForProcessing data: workList) {
service.submit(new Callable<ResultData>() {
public ResultData call() {
// process the data here and return some result (optionally).
}
});
}
// retrieve the results as the become available.
for (int i = 0; i < workLoad.size(); i++) {
// Retrieve the next processed result. CompletionService returns each task in a
// Future which provides you with the means to control the lifecycle of the task.
// When a submitted task is processed by the executor, its Future is placed on a
// BlockingQueue maintained by the CompletionService. Invoking take() will return
// the next available result. If your tasks do not return a result you would still
// need to call Future<Void> f = service.take() in order to wait for every task
// to finish before proceeding.
Future<ResultData> f = service.take();
// If your task returns some result you may access it via the get() method on the
// task's Future.
ResultData result = f.get();
// do some processing if needed
process(result);
}
答案 6 :(得分:0)
我刚刚使用简单的一个全局int参数完成了它。 我喜欢这样:
do{
Thread.sleep(800);
System.out.println("threadCounter=="+threadCounter);
}while(threadCounter != 0);
其中threadCounter
定义全局int
初始化zero
关于制作每个帖子我做threadCounter
;
在我的函数threading
中,我在threadCounter
这里Thread.sleep(800)
因为没有运行do-while循环继续。