Results 1 to 6 of 6
  1. #1
    Lachezar is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default Thread.sleep() question

    Hi,

    It seems a trivial question, but how the static method Thread.sleep() of classThread, knows
    what the current executing thread is, in order to pause it? There is no "this" reference in this method.

    I know, one of the explanation could be " in whatever class/thread the Thread.sleep() is located , that is your current thread. Yes but where is the "this" reference to that "whatever" class/thread?

    Thank you in advance

    --Lachezar

  2. #2
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    It uses static Thread.currentThread() to get a reference to the currently executing thread object :) :) :)

    To make such tricks Java provides special class ThreadLocal.
    This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable
    So this is kind of global variable but each thread has its own instance. See also Thread-local storage - Wikipedia, the free encyclopedia

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You mention "this". "this" refers to the current instance of the object in which the code is running. That has nothing to do with the current thread.

  5. #5
    Lachezar is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Steve ,You said " That has nothing to do with the current thread."-It has!
    Any thread is declared as a part of a class(after all it has to implement "runnable"), and whatever method has an instance of that class ,can run the thread.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Actually, the current thread is an instance of Thread. When you instantiate a Thread, you pass it an instance of Runnable. When the thread instance is started, the Thread class instance calls the run() method of the Runnable class.

    Any code in the Runnable class that uses "this" will return the Runnable instance, not the Thread instance. To get the Thread instance, use the static Thread.currentThread() method. In the case of sleep(), that method is static; the method knows to suspend the thread in which the method was invoked.

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. Thread.sleep/Init?
    By Moncleared in forum AWT / Swing
    Replies: 6
    Last Post: 01-24-2009, 01:31 AM
  3. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 03:27 PM
  4. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 06:14 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •