Reputation: 79
I couldn't find a question similar enough to this, but I do apologize if this is a duplicate.
//do something
//start
new Thread(r).start();
Runnable r = new Runnable() {
public void run() {
try{
Thread.sleep(5000);
//do something
}
catch(InterruptedException e){}
}
};//end new Runnable
My question is: Am I using a thread? or just a runnable object? Not sure if I am even asking a meaningful question?
I am new to threads, but my understanding of this code is : I am starting a new thread, passing it a runnable object, and it begins the overridden run method, in this case the thread will sleep for 5 seconds then do work.
Upvotes: 2
Views: 494
Reputation: 9816
An instance of Thread
(as you create in the first line of code) represents a physical thread. When you call start()
, the JVM will spawn a new thread (using the OS native methods to do so) and start its execution.
A Runnable
is just an interface that defines the run()
method (as you do in your second line of code). If you pass any object implementing Runnable
to the Thread
constructor, then the thread will run the code defined in the run()
method.
Runnable
s are very lightweight since they only define one method, while Thread
s come with the heavy weight of a physical thread.
Directly creating a Thread
is not recommended since it's an expensive operation. I highly recommend you look at the java Executors framework as it provides a flexible way of creating thread pools and submitting tasks (in the form of Runnable
s or Callable
s) to them. The Executors framework provides invaluable facilities for thread reuse.
Upvotes: 2
Reputation: 6184
Runnable
interface. Thread
Thread
(new Thread(r)
) and passed the anonymous instance to thread instancenew Thread(r).start()
), the jvm created a new thread. This new thread in turn invoked the run() method of the anonymous Runnable
implementation. Upvotes: 2
Reputation: 39477
Yes, you're using a Thread. You create it by passing to its constructor a Runnable object.
"I am starting a new thread, passing it a runnable object, and it begins the overridden run method, in this case the thread will sleep for 5 seconds then do work."
This is correct, but the key point is missing: that this run method will be executed in another/new Thread, and not in the Thread from which you called start() on the Thread object which you created.
Upvotes: 2
Reputation: 133619
The Runnable
interface just declares the fact that the class that implements it is able to be executed in a thread, so that it provides the interface to be called and executed by an external thread, nothing more.
A Thread
implements Runnable
since it's able to execute its own run()
method but Runnable in general doesn't have anything related to threading itself. The default run() of Thread doesn't do anything, that's why you usually extend it and define the specific behavior of your thread.
But just to clarify, a ThreadPoolExecutor accepts Runnable
objects exactly like a Thread
. It's just the interface which declares that the class definition, is indeed, runnable (or executable).
Upvotes: 2