-1
Thread t2 = new Thread();
Thread t3 = new Thread();

t1.start();
t2.start();
synchronized (t2) {
    t2.wait(20000);
}
t3.start();

The above program runs without t2 waiting for 20sec. I observed that when thread object is started then it wont wait. Why is this happening?

  • How do you determine that `t2` does not wait? What does `t2` actually do? – luk2302 Sep 20 '20 at 09:43
  • `wait` has nothing to do with telling `t2` to wait. Do some [research](https://www.baeldung.com/java-wait-and-sleep) please. – akuzminykh Sep 20 '20 at 12:48
  • But what actually happens here i dont understand. Thread instance with wait method does not make the current executing thread wait. – Komal Patil Sep 20 '20 at 13:19
  • 1
    @KomalPatil `t2` doesn't do anything. It starts and instantly terminates as there is no code for it to run. So the calling thread of `t2.wait` nearly doesn't wait at all. The only thing it waits for is for `t2` to terminate, which notifies the waiter. – akuzminykh Sep 20 '20 at 13:22
  • Trying to tell some other thread to wait this way is a bad idea from the beginning. In the long run, designing your threads to act as independent units that communicate as peers is an approach more likely to end up with a good and maintainable solution. Trying to micro-manage threads is about as effective as micro-managing software developers. – JensG Sep 21 '20 at 10:48
  • Do not call `wait` or `notify` on a `Thread`, because those methods are used internally to signal thread completion. In your case most likely a `notify` happened when `t2` finished its own `run` method. Use a dedicated monitor object. – Thilo Sep 21 '20 at 10:53

3 Answers3

1

Why is this happening?

First, let's be clear. This function call, t2.wait(20000) does not do anything to the t2 thread. In fact, it doesn't really do anything at all. All it does is not return until either one of two things happens;

  • Some other thread calls t2.notify(), or
  • 20 seconds elapse.

If the call took less than 20 seconds to return, then that's probably because the t2 thread itself called t2.notify() just before it died. In most implementations of the Java standard library, the join() method is implemented by using wait() and notify() calls on the thread object.

(Note: most authors will advise you not to ever call wait() or notify() on a Thread instance precisely because of the potential for interference between your code and the library code when both call the same methods on the same instance.)

The above program runs without t2 waiting for 20sec.

As somebody else already has pointed out here, You have not provided any run() method for your t2 thread, so it's unclear why you would expect the t2 thread to "wait" or, to do anything else at all. The only thing a thread ever does is execute the code that you provide for it in a run() method.

The default Thread.run() method would call the run() method of a delegate object that you supply when you construct the threads, but your code supplies no delegate. In that case, the default run() method does nothing at all.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
0

Use sleep to pause the thread regardless of having work to finish. wait doesn't pause the thread, it just waits the thread to finish (and this thread already finished).

Jonas Fagundes
  • 1,519
  • 1
  • 11
  • 18
0
class SleepThread extends Thread  {    
  //run method for thread
    public void run()    {    
        for(int i=1;i<5;i++)   {    
            try  {  
              //call sleep method of thread
              Thread.sleep(20000);  
            }catch(InterruptedException e){
              System.out.println(e);
            }    

          //print current thread instance with loop variable
          System.out.println(Thread.currentThread().getName() + "   : " + i);    
        }    
    }    
}

class Main{
   public static void main(String args[]){  
     //create two thread instances
        SleepThread thread_1 = new SleepThread();    
        SleepThread thread_2 = new SleepThread();    
    //start threads one by one
        thread_1.start();    
        thread_2.start();    
    }    
}  

Thread sleep method in java

Mova
  • 928
  • 1
  • 6
  • 23