javamultithreadingjava-threadsthread-sleep

Looking for clarity on Thread.sleep() method


Thread.sleep() method puts the thread that is running it to sleep. thread.sleep() statement in the below code will be run by main thread, 'thread' in the below code is an user defined thread. Why is the main thread is sleeping when called on the reference of user defined thread.

public class ThreadDemo {

    public static Thread t1 = null;
    public static Thread t2 = null;
    public static void main(String[] args) throws InterruptedException{

        System.out.println("start of main function, Thread : "+Thread.currentThread().getName());
        t1 = Thread.currentThread(); //main thread
        MyThread thread = new MyThread();
        t2 = thread;
        thread.start();

        System.out.println("Before sleep in main method : "+System.currentTimeMillis());
        thread.sleep(1000);
        System.out.println("After sleep in main method : "+System.currentTimeMillis());

        System.out.println("In main method, Thread : "+t1.getName()+" ,In state: "+t1.getState());
        System.out.println("In main method, Thread : "+t2.getName()+" ,In state: "+t2.getState());

        System.out.println("Is thread, "+thread.getName() + " alive : "+ thread.isAlive());

        System.out.println("end of main function, Thread : "+Thread.currentThread().getName());
    }

    public static class MyThread extends Thread{
        int sum = 0;
        @Override
        public void run() {
            System.out.println("In run method, Thread : "+t1.getName()+" ,In state: "+t1.getState());
            System.out.println("In run method, Thread : "+t2.getName()+" ,In state: "+t2.getState());
            System.out.println("start of run method, Thread : "+Thread.currentThread().getName());
            for(int i=0; i<10000; i++){
                sum += i;
            }
            try{
                Thread.sleep(10000);
                System.out.println("After sleep in run method : "+System.currentTimeMillis());
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("end of run method, "+ Thread.currentThread().getName() +", returns : "+sum);
        }
    }
}

Output:

start of main function, Thread : main
Before sleep in main method : 1701105524066
In run method, Thread : main ,In state: TIMED_WAITING
In run method, Thread : Thread-0 ,In state: RUNNABLE
start of run method, Thread : Thread-0
After sleep in main method : 1701105525070
In main method, Thread : main ,In state: RUNNABLE
In main method, Thread : Thread-0 ,In state: TIMED_WAITING
Is thread, Thread-0 alive : true
end of main function, Thread : main
After sleep in run method : 1701105534071
end of run method, Thread-0, returns : 49995000

I don't understand how main thread is sleeping when called on the reference of user defined thread.


Solution

  • Letting a thread put another thread to sleep would be a security risk so it is not allowed. Thread.sleep is a static method that puts the current thread to sleep.

    One of the more confusing features of the Java language is that you're allowed to call static methods on object instances as if they were instance methods. The actual instance is ignored (it can even be null), the type of the variable is used to figure out what class to call the method on. But that can leave the user thinking they are doing something with a particular instance that they aren't.