0

I have two jobs J3 & J5 where,

  1. J3 starts only after completion of jobs J1 & J2
  2. J5 starts only after completion of job J4
  3. These two jobs(J3 & J5) need to be executed in two different threads.
  4. J3 & J5 threads need to run simultaneously

How can it be programmed in Java?

1
  • The condition on J3 and J5 doesn't make much sense... threads are meant to be non-deterministic. IMHO, the best 'real-world' solution would be to make both J3 and J5 depend on J1, J2 and J4. Commented May 3, 2011 at 6:10

3 Answers 3

3

Are J1, J2 and J4 also threads? You could pass them into your other jobs and use Thread.join() to wait for them to complete. For example the first 3 threads:

Thread j1 = new Job1Thread();
Thread j2 = new Job2Thread();
Thread j3 = new Job3Thread(j1, j2);
// start them up, etc.

public class Job3Thread extends Thread {
    private final Thread j1;
    private final Thread j2;

    public Job3Thread(Thread j1, Thread j2) {
        this.j1 = j1;
        this.j2 = j2;
    }

    public void run() {
        try {
            j1.join();
            j2.join();
            // now start processing
        } catch (InterruptedException ie) {
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

J3 & J5 threads need to run simultaneously. How this can be achieved?
If this is homework I'll leave that part to you ;) You just need to extend what I wrote for J4 and J5.
nope.its not a homework. but I am confused how J3 as one block of task containing J1 & J2 and J5 as one block of task containing J4 can be executed in parallel?
Create J4 and J5 in the same place you create the other threads. Pass J4 to J5 like above. If you start all of the threads at once, they'll run in parallel with your completion rules.
I dont want these two blocks to be waiting
|
0

You could always create a master thread that checks to see what the status is of the various threads are. Once it sees that J1 & J2 are done it then fires off J3. The same logic could be applied for J4 and J5.

J3 and J5 could run in parallel as a result of this.

The status can be determined by placing a boolean value in your threads such as "running".

1 Comment

ya. But I want these two blocks of jobs to be executed in parallel. Master slave is certainly a approach. I admit
0

You didn't say jobs J1 and J2 have to be concurrent so the simplest thing to do is

// thread one
J1();
J2();
J5();

// thread two
J3();
J4();

The simplest way to have one task follow another is to have method calls in the same thread one after they other. ;)

1 Comment

ya this approach i also thought of..but what if J3-> J1 & J2 is one block and J5->J4 is one block and these blocks need to be done in parallel..are u hinting at forking?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.