Jayraj Patel
Jayraj Patel

Reputation: 79

Java code Multiple Thread, Wait to start other

I a trying to find out, after complete first thread completely than start second thread, after complete second thread than start third thread, Please help me!!

Here is my code:

public class wait {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("First Thread");

        createtheard2();
    }

    public static void createtheard2() {

        try {

            System.out.println("Second Thread");

        } catch(Exception error1) {
            error1.printStackTrace();
        }

        createtheard3();
    }

    public static void createtheard3() {

        try {

            System.out.println("Third Thread");

        } catch(Exception error1) {
            error1.printStackTrace();
        }
    }
}

After complete first thread, than start second thread, after complete second thread, than start third thread, Please help me!! Thanks!!

Upvotes: 0

Views: 1048

Answers (3)

Powerslave
Powerslave

Reputation: 1428

As it was pointed out, using threads in this case does not make sense as you are executing tasks sequentially.

However, it is possible to have a single thread running at a time with SingleThreadExecutor

For example, you

  1. Add your threads to a "todo list" (ArrayList will do the job)
  2. Schedule a task (ExecutorService.execute())
  3. Wait for the scheduled task to complete (Thread.join())
  4. Drop the thread from the "todo list" tasks.remove(currentTask);
  5. Pick the next task or go to step 7 if all has been finished
  6. Go back to step 2
  7. Kill the ExecutorService (ExecutorService.shutdown())

Alternatively, you could use ExecutorService.invokeAll() using a single thread executor.

You could even simply run the tasks directly in a loop, invoking start(), however, I'd really recommend against using concurrency where it is not a necessity.

Upvotes: 0

vels4j
vels4j

Reputation: 11298

I think what you need is if task 1 (thread in your terms) success, run task2 else wait for task1. consider the following.

public class Process{

    public static void runProcess1() {

        boolean done = false;
        do {
            // make done=true after task1 is done
        } while (done);
        runProcess2();
    }

    public static void runProcess2() {

        boolean done = false;
        do {
            // make done=true after task2 is done
        } while (done);
        runProcess3();
    }

    public static void main(String[] args) {
        runProcess1();
    }
}

Upvotes: 0

mvb13
mvb13

Reputation: 1594

  1. Implement Runnable

    public class ThreadDemo implements Runnable {  
           public void run() {
              //Do this what you need
           }
    }
  2. Use join to wait while thread will be completed.

    Thread t1 = new Thread(new ThreadDemo());
    // this will call run() function
    t1.start();
    // waits for this thread to die
    t1.join();
    
    Thread t2 = new Thread(new ThreadDemo());
    // this will call run() function
    t2.start();
    // waits for this thread to die
    t2.join();

From http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html

t.join() causes the current thread to pause execution until t's thread terminates.

In your case paused by join method invocation thread will be Main thread.

Upvotes: 2

Related Questions