2

这样我正在生成线程,但是在 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是我的函数,它对我传递的不同数据执行

4

7 回答 7

4

您可以使用ExecutorService。事实上,我强烈推荐这个。您可以指定不同的线程行为等。执行器将为每个提交的线程返回一个Future对象,然后您可以遍历每个Future收集结果。所以以下内容:

for (Future f : futures) {
   f.get(); // get a result here...
}

将在所有Futures返回数据时完成(即当线程完成时)。Executor API 的工作级别比join()/ notify()etc 更高,使用起来更安全。

这里有很多功能可以使用,我会推荐教程

于 2012-09-28T10:18:12.227 回答
2

你可能想要join线程。参照。http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html。鉴于您可以暂停主线程,即。

于 2012-09-28T10:18:01.297 回答
1

使用ExecutorService

- Executors很好地管理Thread对象和Async任务

-这里不是直接运行线程的客户端,而是中间对象。

-submit()将此与返回Future对象的方法相结合。

- Future是异步任务的结果

-您可以使用get()Future的方法,它具有阻塞性质,或者get()方法与isDone()方法一起使用。

于 2012-09-28T10:21:55.573 回答
0

使用Thread类中的isAlive方法。

从文档中:

测试此线程是否存在。如果线程已启动且尚未死亡,则该线程处于活动状态。

于 2012-09-28T10:17:59.883 回答
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....
于 2012-09-28T10:19:31.843 回答
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);
}
于 2012-09-28T10:37:32.887 回答
0

我只是用一个简单的全局 int 参数完成了它。我就是这样做的:

do{
    Thread.sleep(800);
    System.out.println("threadCounter=="+threadCounter);
    }while(threadCounter != 0);

在哪里threadCounter定义全局int初始化zero

在制作我做+1的每个线程时threadCounter

在我的职能threading中,我做-1threadCounter

这里Thread.sleep(800)不运行那个do-while循环继续。

于 2012-10-01T14:28:39.713 回答