0

I have a class ThreadClass which looks as follows:

public class ThreadClass extends Thread{

    String msg;

    public void run()
    {
        for(int i=0;i<=5;i++)
        {
            System.out.println("Run method: "+msg);
        }
    }

    ThreadClass(String mg)
    {
        msg=mg;
    }

}

public class MainThreadClass {

    public static void main(String[] args) {

        ThreadClass dt1=new ThreadClass("Thread 1");
        ThreadClass dt2=new ThreadClass("Thread 2");

        dt1.start(); 
        dt2.start(); 

        System.out.println("Finished");
    }
}

the output I am getting is:

Run method: Thread 1
Finished
Run method: Thread 1
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 1
Run method: Thread 1
Run method: Thread 1
Run method: Thread 1

The output I would like to achieve would be:

Run method: Thread 1
Run method: Thread 1
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 2
Run method: Thread 1
Run method: Thread 1
Run method: Thread 1
Run method: Thread 1
Finished

so the string finished is printed out when these two threads terminate. How to do it?

3 Answers 3

1

Wait for each thread to exit using join():

public class MainThreadClass {
  public static void main(String[] args) {

    ThreadClass dt1=new ThreadClass("Thread 1");
    ThreadClass dt2=new ThreadClass("Thread 2");

    dt1.start(); 
    dt2.start(); 

    dt1.join();
    dt2.join();

    System.out.println("Finished");
  }
}

[Sorry about the lousy formatting, for some reason I can't get this looking any nicer. IE issue maybe?]

Sign up to request clarification or add additional context in comments.

1 Comment

@Radek - can't see the output you posted here, but you cannot expect deterministic interleaving of the output from your child threads unless you enforce this using locking.
0

You need to determine when a thread was finished:

// Create and start a thread
Thread thread = new MyThread();
thread.start();

// Check if the thread has finished in a non-blocking way
if (thread.isAlive()) {
  // Thread has not finished
} else {
  // Finished
}

// Wait for the thread to finish but don't wait longer than a
// specified time
long delayMillis = 5000; // 5 seconds
try {
   thread.join(delayMillis);

  if (thread.isAlive()) {
     // Timeout occurred; thread has not finished
  } else {
     // Finished
  }
 } catch (InterruptedException e) {
    // Thread was interrupted
}

// Wait indefinitely for the thread to finish
try {
 thread.join();
  // Finished
} catch (InterruptedException e) {
  // Thread was interrupted
}

Comments

0

Or use an ExecutorService. Here is a excerpt from a class I have that does similar to yours.

ExecutorService l_service = Executors.newFixedThreadPool(l_number_threads);
List<Future<T>> l_results = null;
try {
  l_results = l_service.invokeAll(a_tasks);
} catch (InterruptedException ex) {
  throw ex;
}
l_service.shutdownNow();

Comments

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.