0

E.g. method public static void sleep(long millis). This method causes current thread to sleep, but how does it know, which thread is current? Static methods are object-independent and belong to class, so how does this mechanism work?

1
  • @Downvoter, comment your action, please Commented Feb 19, 2013 at 11:57

5 Answers 5

3

This method causes current thread to sleep, but how does it know, which thread is current?

The current thread is managed by the underlying operating system (or the threading system). The underlying (system dependant) thread implementation provides a handle to the current thread:

  • In case of POSIX threads, there is the API call pthread_self() which returns the thread ID of the currently executing thread. You can think of Thread.currentThread() eventually calling this C function and wrap the thread ID in a Java Thread object which is then returned.

  • In case of MS Windows, there is the API call GetCurrentThread() which returns the Windows Handle of the currently executing thread. Again, you can think of Thread.currentThread() eventually calling this C function and wrap the Handle in a Java Thread object which is returned.

Sign up to request clarification or add additional context in comments.

6 Comments

Question is still the same - how does currentThread() work if it static and belongs to class, not to concrete Thread object?
There is a concrete Thread object (means, data associated with the thread), but it is managed by the operating system (or the threading system). Through the static methods, you basically get a Java wrapper for the corresponding "current" thread.
great answer, thank you! But, please, pay questioner's attention on edits, I saw your edit too late to accept
You could still revoke and accept a different answer - @Anony-Mousse, I certainly don't want to revoke reputation from you, just wanted to mention that it is still up to the questioner to accept a different answer if it fits better :)
+1. Essentially: the OS knows - it dispatched the thread in the first place.
|
3

native methods are special. They have access beyond the Java API.

In this case, to the current thread. You can't use this method to put another thread to sleep - the other thread will have to do this by itself (but you can send it a message, obviously).

2 Comments

I cry every time somebody says me: "It is beyond the Java API, don't touch this". Thank you, I forgot to check java sources.
It's not beyond the Java API. It's actually not implemented in Java at all, but in dirty, messy, highly optimized C code. I wasn't trying to say "stay away from this", but I was trying to say "don't try to apply your Java rules to this method, it's not Java".
0

This method is always called for the current / executing thread.

Comments

0

It means your current thread will go in sleep mode for a perticlar time. Ex: if i will write Thread.sleep(1000) thread will go to sleep state for 1000 miliseconds. We use this menthod mainly to interchange between the thread.In short, it will give chance to another thread for execution.

1 Comment

See comment to @Sudhanshu Umalkar answer
0

The current thread is the one actually executing the piece of code. As simple as that.

For instance:

public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { System.out.println("Before sleeping!"); Thread.sleep(10000); System.out.println("After sleeping!"); } } Thread t1 = new Thread(r); Thread t2 = new Thread(r); System.out.println("Main thread!"); } 

May output something like:

Before sleeping! // t1 Main thread! // main Before sleeping! // t2 After sleeping! // t1 After sleeping! // t2 

1 Comment

Does it provide answer to my question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.