wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Mock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Mock" Watch "Threads Mock" New topic
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: 4344
    
    8

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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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.
 
Consider Paul's rocket mass heater.
 
subject: Threads Mock
 
Similar Threads
Help Required with this code on threads
Does program terminate when main thread terminates ?
Doubt in Thread run method.
Number of threads DOUBT