Radek
Radek

Reputation: 1413

Java Threads problem

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?

Upvotes: 0

Views: 65

Answers (3)

Steve Townsend
Steve Townsend

Reputation: 54178

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?]

Upvotes: 1

Mitch
Mitch

Reputation: 1039

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();

Upvotes: 0

Alex
Alex

Reputation: 249

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
}

Upvotes: 0

Related Questions