Given a class that implements the runnable interface
public class MyRunnable implements Runnable {
public void run() {
try {
Thread.sleep(1000);
System.out.println("ok");
} catch (InterruptedException e) {}
}
}
When we run the following code
public class Run {
public static void main(String[] args){
Thread t = new Thread(new MyRunnable());
t.start();
}
}
I don't understand how we can call the static method sleep
of the Thread
class from our run method. It just seems like magic... and I am finding it hard to get an intuitive understanding of what is actually going on.
My assumption is that as the Thread
instance contains a reference to the MyRunnable
instance, the Thread.sleep()
method is a call to a scheduler? that can resolve the call as it can infer which thread this method was called from, allowing the scheduler to pause the thread?