2

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?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
beoliver
  • 5,579
  • 5
  • 36
  • 72
  • 2
    Downvotes? seriously guys? Interesting question about how multi-threading works... – VLEFF Jul 07 '15 at 13:59
  • http://stackoverflow.com/questions/24358989/how-thread-sleep-works-internally – Sotirios Delimanolis Jul 07 '15 at 14:01
  • Never in the life a question is duplicated... the only thing that gets duplicated is a answer perhaps.. and Solltirios show the path.. hope it helps. – Victor Jul 07 '15 at 14:03
  • 1
    @Victor, None of the answers explain how you can just call a static method of a class and it just *work*. – beoliver Jul 07 '15 at 14:06
  • @ThemanontheClaphamomnibus `Thread.sleep` is a native method that gets the currently executing thread - the thread making the call, and then calls the OS to sleep the thread ... does that help? – yas Jul 07 '15 at 14:13
  • The same question effectively - But did not answer the how - http://stackoverflow.com/questions/22104283/if-thread-sleep-is-static-how-does-individual-thread-knows-it-is-put-to-sleep – Sh4d0wsPlyr Jul 07 '15 at 14:17
  • @Sh4d0wsPlyr, yep! now that is the **duplicate**. But why close the question if no one has ever given a proper answer? – beoliver Jul 07 '15 at 14:20
  • 1
    `sleep()` and the other static methods are native methods, so to get a "true" answer you're going to need to dive down into platform-specific code. And even then, I'm not sure if it's handled by the JVM; could be the OS. – awksp Jul 07 '15 at 14:36
  • @ThemanontheClaphamomnibus sometimes the community just doesn't understand that you came here with a question like any person in the life and you only a seeking for an answer... don't bother trying to understand why mark as duplicate.... as i said before, i have never hear a question twice... just try to get to someone that really has will to help or, at least, show a way. To the question concerns... i got no clues of how operate behind the curtains the sleep method, its blocks and it doesn't release monitors if you have previously acquire them, also it honors thread interruption. – Victor Jul 07 '15 at 14:42
  • Re, _None of the answers explain how you can just call a static method of a class and it just work._ That's got to be the broadest question I have ever seen on this site. How can a method call _work_?. That's a book-level topic for sure---multiple books if you want to know _all_ the ways. How _can_ a method call work?. Where would we be if it was _not_ possible for method calls to work? – Solomon Slow Jul 07 '15 at 17:54
  • @jameslarge I am honoured! Do I get a badge? My point was that compared to some of the fantastic answers that you can find on this site, that explain (or link clearly to relevant material) the answers were lacking. Of course it takes time and effort to answer and I do not expect an essay for **my** question. Anyway, clearly it has been linked as a duplicate. So people who are wondering can also be pointed toward an simplistic 'answer'. BTW, I don't think the sentence you quoted was soooo dramatic :) – beoliver Jul 07 '15 at 18:10

0 Answers0