A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Threads and Synchronization
Joined: Nov 18, 2006
Nov 24, 2006 20:31:00
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?
Joined: Jun 17, 2001
Nov 24, 2006 21:08:00
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.
Thread-1 and Thread-2 are simultaneously executing the below code path:
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.
Joined: Aug 17, 2001
Nov 25, 2006 17:15:00
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
Sun Certified Programmer for the Java 2 Platform
Joined: Aug 03, 2004
Nov 29, 2006 02:18:00
Thread.getCurrentThread() always runs within some
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:
subject: Current Thread
synchronization: whats the answer
What causes InterruptedException? How to recover?
is it a DeadLock Condition?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014