-4
class A
{
    public void func()
    {
        new Thread()
        {
            public void run()
            {
                // statements
            }
        } .start();
        new Thread()
        {
            public void run()
            {
                // statements
            }
        } .start();
        new Thread()
        {
            public void run()
            {
                // statements
            }
        } .start();
        new Thread()
        {
            public void run()
            {
                // statements
            }
        } .start();
    }
}

Here, I'm trying the first two threads (say pair A) to run concurrently and the next two threads (say pair B) to run concurrently only after pair A has finished executing.

Also if anybody can explain if it can be achieved via java.util.concurrent or threadgroup. I'll really appreciate any help done.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vanessa
  • 7
  • 4

2 Answers2

0

You can use a CountDownLatch to wait until a certain number of threads call countDown(), at which point the main thread can then continue. You will have to make some alterations to your threads -- namely, you'll need to pass them the latch, and you'll need to have them call latch.countDown() when they are done with their jobs so the latch's count will eventually reach 0.

So your main class would look something like:

final CountDownLatch latch = new CountDownLatch(2); // Making this final is important!
// start thread 1
// start thread 2
latch.await(); // Program will block here until countDown() is called twice
// start thread 3
// start thread 4

And your threads would look something like:

new Thread() {
    public void run() {
        // do work
        latch.countDown()
    }
}

And only once the first two threads finish will the latch allow the main thread continue and start the other two threads.

awksp
  • 11,764
  • 4
  • 37
  • 44
  • Can you please explain a bit how will the threads call countDown? – Vanessa May 15 '14 at 21:11
  • `.countDown()`. I'll expand a bit in my answer – awksp May 15 '14 at 21:12
  • Just as a bit of a warning, it's going to involve a bit more changes than jspurim's answer, which is simpler than mine and I believe would work for your case. `CountDownLatches` would be more useful if you weren't handling the `Thread` objects yourself. – awksp May 15 '14 at 21:16
  • Ohk but I'm curious about it if you can explain. Also can you tell me why people down-voted? – Vanessa May 15 '14 at 21:20
  • Curious about what part? Ask specific questions, and I'll edit my answer. As for downvotes, it's probably because this is a question that is easily Google-able – awksp May 15 '14 at 21:22
  • Got your point. Thanks for some new lessons. I'm sorry I'm not able to upvote but +1 anyways. :) – Vanessa May 15 '14 at 21:24
  • @Vanessa No problem! You sure you don't want me to expand on something in my answer? Don't want to leave any questions unanswered. – awksp May 15 '14 at 21:26
  • Yes sure. Thanks again. For simplicity I'll rate the another one as suggested by you. – Vanessa May 15 '14 at 21:28
  • @Vanesa Even if you can't upvote, you can always accept an answer. It even gives you a reputation bonus. Check this: http://stackoverflow.com/tour – jspurim May 15 '14 at 21:36
0
public void func()
{
    Thread a = new Thread()
    {
        public void run()
        {
            // statements
        }
    }
    Thread b = new Thread()
    {
        public void run()
        {
            // statements
        }
    }
    a.start();
    b.start();
    a.join();  //Wait for the threads to end();
    b.join();
    new Thread()
    {
        public void run()
        {
            // statements
        }
    } .start();
    new Thread()
    {
        public void run()
        {
            // statements
        }
    } .start();
}
jspurim
  • 925
  • 8
  • 25
  • But does it guarantee that a and b will execute concurrently? And also the further two threads? – Vanessa May 15 '14 at 21:18
  • @Vanessa Yes and yes. – awksp May 15 '14 at 21:19
  • 1
    "does it guarantee that a and b will execute concurrently?" No! It _allows_ them to run concurrently, and they _probably_ will execute concurrently if they take any significant amount of time, but there is no _guarantee_ that that is how it will happen. The '.join()' calls do guarantee that func() will not start the second pair of threads until the first pair have terminated. – Solomon Slow May 16 '14 at 04:06