| Author |
Threads Mock
|
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
Source :- cert pal
I can understand that only thread can make itself sleep. But how compiler decodes the line 5 ??
Is there any other way of sleeping the main thread ??
|
SCJP 6.0 96%
(Connecting the Dots ....)
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3786
|
|
That's one of the tricks the exam can play on you.
sleep is a static method of Thread. It always acts on the current thread. In any sensible implementation you would just use Thread.sleep().
But, transistor is a Thread object (because Transistor extends Thread). You can refer to a static member via an instance of the class. So it's a perfectly acceptable piece of code as far as the compiler is concerned. It's bad style, but the compiler doesn't care about that.
|
 |
Tom Reilly
Rancher
Joined: Jun 01, 2010
Posts: 618
|
|
To make the current thread sleep:
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
Thanks Matthew Brown , for excellent,clear and succinct explanation !!!
Thanks Tom reilly.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Tom Reilly wrote:To make the current thread sleep:
What's the use of currentThread() here?? As Matthew pointed out, sleep() is a static method in Thread class, so you can directly call it as Thread.sleep(), it doesn't matter on which Thread instance you call it, it'll always sleep the Thread that called the method...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Tom Reilly
Rancher
Joined: Jun 01, 2010
Posts: 618
|
|
|
Mathew is right. I am wrong. I actually started my post before Mathew replied but got distracted. Had I refreshed before my reply, I would have canceled it.
|
 |
 |
|
|
subject: Threads Mock
|
|
|