File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Mock" Watch "Threads Mock" New topic

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

Joined: Apr 06, 2010
Posts: 4541

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

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

Joined: Aug 03, 2008
Posts: 9453

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

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.
I agree. Here's the link:
subject: Threads Mock
It's not a secret anymore!