File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Thread using Runnable Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread using Runnable Interface" Watch "Thread using Runnable Interface" New topic
Author

Thread using Runnable Interface

Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
I know there are quite a few ways of creating a thread in java. I have used the "Extending Thread Class" method and checked out various methods like getName(), sleep(), etc.

However, how can i use these methods if I am creating a thread by implementing the Runnable interface. And, so if i dont extends Thread class, I get symbol not found error for methods like sleep. Could anyone suggest how to go about doing this?


SCJP 1.4 85%
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Congratulations for shifting from subclassing Thread to directly implementing Runnable. There's almost never a reason to subclass Thread.

As for your question, isn't sleep a static method of Thread?


There is no emoticon for what I am feeling!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

And if you want a reference to the thread that is running your Runnable object, the static "Thread.currentThread()" method gives you that.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As you've discovered, the Runnable interface only has one method: run. In fact, Thread implements Runnable.

So a Thread is a Runnable, but a Runnable is not necessarily a Thread. And to call Thread methods, you need a Thread reference. You get this by passing the Runnable to Thread's constructor.

Thread myThread = new Thread(myRunnable);

After that, you can call Thread methods on the myThread reference.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jeff Albertson:
...isn't sleep a static method of Thread?

That's a good point. As a static method, sleep should be called from the Thread class instead of a particular instance.

So I should have said, "to call instance methods of Thread, you need a Thread reference."
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Thanks. I will try to implement what has been suggested.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You might notice that Runnable is in no way related to Threads, however, Threads are related to Runnable (see the java.lang.Thread class for its dependency).

A Runnable is the closest thing to what other languages call a "closure" - or if you are familiar with .NET, a "delegate". Simply, it is a piece of executable code that can be passed around for execution. Sometimes it is often called a "callback", however, this implies more context than is required to talk about it in this context.

A Runnable simply allows you to pass "some piece of code" for some arbitrary thread to execute it. That it does so by "calling back on the run method" is irrelevant to the more abstract definition of how and why things work. However, what is of importance is that you have separated "the piece of code to execute" and "the thread(s) that will execute it".


Tony Morris
Java Q&A (FAQ, Trivia)
 
Don't get me started about those stupid light bulbs.
 
subject: Thread using Runnable Interface