0

Basically, I have several threads each completing tasks.

Although I have multithreaded the tasks within this subroutine to speed up overall program speed.

I need this subroutine to only close when all threads within it are finished, as after this subroutine is complete the information generated is required by the next subroutine to run in the program.

In contrast to what happens now, which is allowing the subroutine to close and leaving contained threads to run in their own time, in the background. Which forces the next subroutine to run, to only act on partial data.

How do I do this?

Code:

public void MainSetupTasks() throws ClientProtocolException, IOException {

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            // Tasks to be complete

        }
    }).start();

}
  • Sounds like you should get familiar with the Java Concurrency docs: http://docs.oracle.com/javase/tutorial/essential/concurrency/ – Kon Apr 13 '14 at 23:52
  • you want each one to wait the other ? or you want to get an event when all of them are done ? – ahmed_khan_89 Apr 13 '14 at 23:55
  • Well, why don't you use an `ExecutorService` instead? – fge Apr 14 '14 at 00:04
  • I've never used "ExecutorService" before, could you please show a code example? –  Apr 14 '14 at 00:11
  • if i m getting your problem,You need Cyclicbarrier or countdownlatch.is it not possibble by join? – Dwijendra Apr 14 '14 at 04:17

1 Answers1

1

To solve your immediate problem, you could use something like How to wait for a set of threads to complete?

But this is pretty ugly. The built-in ExecutorService lets you launch background threads in a much cleaner way.

Community
  • 1
  • 1
dmcauslan
  • 398
  • 2
  • 8
  • ExecutorService seems like the much better choice, do you mind showing a code example? –  Apr 14 '14 at 00:33
  • [This](http://tutorials.jenkov.com/java-util-concurrent/executorservice.html) looks like a pretty good tutorial. – dmcauslan Apr 14 '14 at 00:37