-2

Lets say I have 3 threads which implement a Runnable interface, and I've started them using below statements.

t1.start();
t2.start(); 
t3.start();

Is there any way using join(), wait(), notify() combination to make threads end in this order t3 , t2, t1 ?

9
  • Unclear. Threads usually terminate, when their run methods terminate. What exactly are you trying to achieve? Commented Oct 1, 2018 at 6:49
  • Given that threads are started at same time, I need a working code in such a way that threads end in mentioned order using join/wait/notify. Commented Oct 1, 2018 at 6:51
  • 3
    This is what you already told us. And I asked you: What problem are you trying to solve? Threads run asynchronously. So why think about an ordering, especially an order about ending those threads? Your question looks like an XY problem. Commented Oct 1, 2018 at 6:53
  • 1
    You can do this control flow a number of ways. Do you want a producer consumer model? Or you could use a Semaphore or countdown latch? Why do you have 3 threads? Could you need 5 threads? What sort of control do you need, as in how would t1 know when it is finished and t3 etc. Commented Oct 1, 2018 at 7:07
  • 1
    so what you want to do is not to terminate in a specific order, but to start in a specific order Commented Oct 1, 2018 at 7:42

2 Answers 2

2

Nope, the threads will terminate depending on what happens inside of their run() method implementations. If you're simply given a currently-running thread without any access to the code it's executing, there is no way to force it to terminate (aside from calling stop() or destroy(), which are both deprecated and should not be used).

If you can write the code that these threads are running, however, it'd be really simple:

Thread t3 = new Thread(() -> { 
    System.out.println("Thread 3 terminating..."); 
});

Thread t2 = new Thread(() -> {
    try { t3.join(); } catch(InterruptedException e) {}; 
    System.out.println("Thread 2 terminating..."); 
});

Thread t1 = new Thread(() -> {
    try { t2.join(); } catch(InterruptedException e) {}; 
    System.out.println("Thread 1 terminating..."); 
});

t1.start();
t2.start(); 
t3.start();
Sign up to request clarification or add additional context in comments.

2 Comments

but be sure to put joins at the end of your run method, otherwise you just will block a thread till the others finishes, here you are not asking threads to finish, but you ask them not to finish before others..
This works. Thanks.
-1

Sounds like you might want a producer consumer model.

BlockingQueue<Callable<Boolean>> queue1 = new LinkedBlockingQueue<>();

Thread t1 = new Thread(()->{
   //do work.
   queue1.add(()-> someTask() );
   //do more work.
   queue1.add(()-> someTaskThatReturnsFalse() );
}).start();

Thread t2 = new Thread(()->{
     boolean shouldBeRunning = true;
     while(shouldBeRunning){

        Callable<Boolean> r = queue1.poll();
        shouldBeRunning = r.call();
     }
}).start();

You can have as many queues as you like. There are other ways too using locks and barriers. This could be done using join/wait/notify but you would essentially be re-creating the LinkedBlockingQueue.

1 Comment

I'm not sure why this answer would be non-useful. From ops description "...each one populates different results" Which sounds exactly like a producer. "other threads have to wait until results are available " which sounds like a consumer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.