2

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.

3
  • 2
    The concept of Threads always is confusing. But yes anything run inside run() will be a separate Thread. Commented Dec 6, 2013 at 19:27
  • Lets say "new Thread(r).start();" is inside a loop. Each time this statement executes does a new Thread begin or does the current Thread become the new one? Commented Dec 6, 2013 at 19:30
  • you say new every time. So as long you keep using word new in your code then yes new thread is created in the for loop. Commented Dec 6, 2013 at 19:35

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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).

Comments

2
  1. You created a new anonymous implementation of Runnable interface.
  2. This implementation povided the action to perform when executing the this implementation in a new Thread
  3. You created a new Thread (new Thread(r)) and passed the anonymous instance to thread instance
  4. When you started executing the thread (new Thread(r).start()), the jvm created a new thread. This new thread in turn invoked the run() method of the anonymous Runnable implementation.

Comments

2

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.

Runnables are very lightweight since they only define one method, while Threads 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 Runnables or Callables) to them. The Executors framework provides invaluable facilities for thread reuse.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.