-4

My problem is, I have two threads t1 and t2. Both of them make some calculations, and according to my program, I want to use a concurrency technique that blocks till t1 and t2 both finish their tasks and then continue.

I tried countdownLatch, and I read about ExecutorService and made a small example. Concerning the ExecutorService I did something like the following:

executor.execute(new RunnableClass(bgr,3))
executor.execute(new RunnableClass(bgr,7))
executor.shutdown();

if (executor.isTerminated()) {
    print("terminated") 
}

and the word "terminated" was never printed, which means executorService object does not block.

please let meknow whih concurrency technique i should use to suit my situation

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 1
    Seriously, read the API doc. The answer is in there: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#shutdown%28%29 – JB Nizet May 03 '15 at 13:54
  • `isTerminated()` does not check if the executor was shutdown, it checks if it is terminated. As JB Nizet says, read the documentation, it's pretty clear. If you have any doubts about the doc, let us know. – m0skit0 May 03 '15 at 14:00
  • @m0skit0 i know that the isterminated() does not check if the executor was shutdown or not..i want to block till t1 and t2 finishes execution so that i cn continue..please advise – Amrmsmb May 03 '15 at 14:03
  • Use `awaitTermination`, as stated in the API doc. – duckstep May 03 '15 at 14:06
  • As already suggested in the first comment: read the documentation. – m0skit0 May 03 '15 at 15:44

1 Answers1

0

Reading the documentation is always going to clarify things before you start coding.. But just wanted to share a few points : 1.) CountDownLatch can be a solution for your problem. Read this link before you code.. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html Basically the concept is that you have latch your program in a fixed state and stop it from proceeding until the latch is released. So you have 2 threads...set a countdownlatch value of 2. Make both the threads decrement the value after they are done. Till the latch value is decremented to 0 the main thread will be waiting for this to take place.

executor.shutdown....refer documentation...it says that it waits for all submitted tasks to complete before it shutsdown.

acearch
  • 343
  • 1
  • 8