Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread using Runnable Interface

 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I will try to implement what has been suggested.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic