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を使用できます。実際、これを強くお勧めします。さまざまなスレッド動作などを指定できます。エグゼキューターは、送信されたスレッドごとにFutureFutureオブジェクトを返し、結果を収集するたびに反復処理できます。したがって、次のようになります。

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

すべてがFuturesデータを返したとき (つまり、スレッドが完了したとき) に完了します。join()Executor API は/notify()などよりも高いレベルで機能し、使用するのがはるかに安全です。

ここには利用できる機能がたくさんあります。チュートリアルをお勧めします。

于 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管理するThreadAsync

-ここでは、スレッドを直接実行するクライアントではなく、中間オブジェクトです。

-これをオブジェクトsubmit()を返すメソッドと組み合わせる。Future

- 非同期タスクFuture結果です。

-ブロックの性質を持つget()Future のメソッド、またはメソッドとメソッドを組み合わせて使用​​できます。get()isDone()

于 2012-09-28T10:21:55.573 に答える
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

私は単純な 1 つのグローバル int パラメータでそれを行いました。私はこのようにします:

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

where threadCounterdefine global intinitializezero

すべてのスレッドの作成時に、私は +1 を行いthreadCounterます。

での私の機能threadingでは、私は -1 を行いますthreadCounter

ここThread.sleep(800)では、do-while ループが継続することを実行しません。

于 2012-10-01T14:28:39.713 に答える
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 に答える