0

I want to count Words with several Threads and every thread has a "wordCount" variable. Now how can i wait till every thread is finished? At the end i should count every variable of each thread together and print it out?

How i create my Threads:

for (int i = 1; i <= THREADS_TO_USE; i++) {
    long bytesToRead = splitManager();
    new Thread(new CountWordsThread(file, bytesToRead, bytesCounted)).start();
}
vsminkov
  • 10,912
  • 2
  • 38
  • 50
S A D B O Y S
  • 23
  • 1
  • 4
  • 4
    You might want to look into `ExecutorService` or even `ForkJoinPool`. – Mena Aug 31 '16 at 13:21
  • 1
    A `CountDownLatch` can be used to wait until all the threads finish their work before allowing the main thread to continue it's work – Slimu Aug 31 '16 at 13:22
  • Instead of creating new Threads, submit your Runnables to an ExecutorService, then call the ExecutorService’s `shutdown` method followed by a call to its `awaitTermination` method. – VGR Aug 31 '16 at 14:12

0 Answers0