File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Current Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Current Thread" Watch "Current Thread" New topic
Author

Current Thread

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Isn't there more than one current thread? How can Thread methods be static and apply to the current thread and how can Thread.getCurrentThread() work on a computer that has 4 processors and up to 4 threads running simultaneously?

-- Kaydell
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
Distinguish between code blocks and threads of execution. Multiple threads of execution could be executing the same code blocks.

CurrentThread referes to the thread that is executing that code block and not to all threads that are running in the JVM at that time.

Example:
Thread-1 and Thread-2 are simultaneously executing the below code path:

{
System.out.println(Thread.currentThread());
}


They both end up executing the System.out.println(). For Thread-1's execution path, CurrentThread is Thread-1. Similarly for Thread-2.

I hope I understood your question right.
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Thread.currentThread() is static, so that you can get a reference to your thread, without having an instance of your Thread already (That would be kind of pointless )

Geoffrey


Sun Certified Programmer for the Java 2 Platform
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Thread.getCurrentThread() always runs within some Java thread and it returns Thread object for that thread.

In other words Thread.currentThread() returns a reference to Thread from which it is called.
[ November 29, 2006: Message edited by: Vlado Zajac ]
 
 
subject: Current Thread