This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Mock

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make the current thread sleep:
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew Brown , for excellent,clear and succinct explanation !!!
Thanks Tom reilly.



 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic