• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Current Thread

 
Ranch Hand
Posts: 693
Mac OS X Eclipse IDE Firefox Browser
  • 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
 
Ranch Hand
Posts: 60
  • 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.
 
Ranch Hand
Posts: 171
1
  • 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
 
Ranch Hand
Posts: 245
  • 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 ]
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!