0
main() {
     /* code calling another class method   
    that use multi-threading*/ // A block

    sysou("print");    //B block
}

The way it is right now, even though A block got processed 1st, then B block has been executed and the remaining threads of A block.

I want to execute the B block, after all the treads created in A block have finished executing.

Gray
  • 115,027
  • 24
  • 293
  • 354
OnePunchMan
  • 720
  • 15
  • 33
  • 1
    What research have you done? What have you tried? – Gray Oct 15 '13 at 19:45
  • 1
    this [example(s)](http://arashmd.blogspot.com/2013/07/java-thread-example.html#ai) may help. –  Oct 15 '13 at 19:53
  • @Gray ya I know your intention, mocking newcomers. I am fed up of guys like you. Check this answer http://stackoverflow.com/a/18517555/2508414 and compare with the answer given above in that question – OnePunchMan Oct 15 '13 at 20:21
  • 2
    Wow dude. You shouldn't get so defensive. I would respond the same way to an expert. SO isn't your research assistant. One requirement is that questions show "... attempted solutions, why they didn't work, and the expected results". See here: http://stackoverflow.com/help/on-topic – Gray Oct 15 '13 at 20:26
  • 2
    I see nothing suggesting "mocking" in @Gray's initial comment. You may be reading a lot more into his brief and valid statement than was intended. – Hovercraft Full Of Eels Oct 15 '13 at 21:45
  • There are free resources for learning things like this, like [here](http://docs.oracle.com/javase/tutorial/essential/concurrency/). So try on your own before asking for us to give you the answer. – Kevin Panko Oct 16 '13 at 00:49

1 Answers1

3

Consider using a CountDownLatch. This was built explicitly for this purpose.

Per the API:

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • synchronization works for only between the threads, but the B block is not related to the A block threads. – OnePunchMan Oct 15 '13 at 19:50
  • 1
    @kaze: your statement doesn't apply as a CountDownLatch is not the same as using a synchronized method or block. Please read the API (the link is in the answer) and you'll see that this will work. – Hovercraft Full Of Eels Oct 15 '13 at 19:51
  • 1
    @kaze This is between Threads. the main method runs in a thread. It's called the main thread. – Cruncher Oct 15 '13 at 19:54