• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current Thread

 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vlado Zajac
Ranch Hand
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic