2

It may be a basic question, i was confused with this,

in one file i have like this :

public class MyThread extends Thread {
    @Override
    public void run() {
        //stuffs
    }
}

now in another file i have this :

public class Test {
    public static void main(String[] args) {
        Thread obj = new MyThread();

        //now cases where i got confused
        //case 1
        obj.start();   //it makes the run() method run
        //case 2
        obj.run();     //it is also making run() method run
    }
}

so in above what is the difference between two cases, is it case 1 is creating a new thread and case 2 is not creating a thread ? that's my guess...hope for better answer SO guys.Thanks

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • There is a nice link which covers thread fundamentals : http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html – rai.skumar Nov 29 '12 at 06:56
  • @rai.skumar thanks for the link, but that is on concrrency, not what i asked here. – Android Killer Nov 29 '12 at 07:18
  • What "better answer" were you hoping for? Here's the short answer: start() is the method that the library provides for you to call when you want to start a thread. run() is the method that _you_ provide for the library to call, that determines what the thread will do. – Solomon Slow Jul 21 '14 at 13:25

7 Answers7

17

start() runs the code in run() in a new thread. Calling run() directly does not execute run() in a new thread, but rather the thread run() was called from.

If you call run() directly, you're not threading. Calling run() directly will block until whatever code in run() completes. start() creates a new thread, and since the code in run is running in that new thread, start() returns immediately. (Well, technically not immediately, but rather after it's done creating the new thread and kicking it off.)

Also, you should be implementing runnable, not extending thread.

Community
  • 1
  • 1
Corbin
  • 33,060
  • 6
  • 68
  • 78
2

Calling start() will create a new execution thread, and then the run() will be executed in the newly created thread

where as directly calling run() will execute the code in the current thread

Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
2
  • run() method after calling start() : this is executed by your thread which is created by you, it is allocated processor to run independently.

  • run() method called by you : executed from your calling thread.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
2

the simple answer to your question is this:

run(): runs the code in the run() method, blocking until it's complete

start(): returns immediately (without blocking) and another thread runs the code in the run() method

Brian Dilley
  • 3,888
  • 2
  • 24
  • 24
2

Calling start starts the thread. It does the underlying work to create and launch the new thread, and then calls run on that new thread.

Calling run just calls the run method, on the current thread. You never call run directly, use start.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

In one line directly calling run() is synchronous (your code will block until run() returns) and calling start() (your code will not wait for run to complete as it is called in other thread obj) is asynchronous.

When you directly use start() method, then the thread will run once using the Runnable instance you provided to it and then the thread will be unusable.

But to leverage the Thread Pooling and Scheduling capabilities that are inbuilt in Java, extending a Runnable or Callable is the way to go.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
1

start() starts the thread. run() just runs the code, in the current thread.

user207421
  • 305,947
  • 44
  • 307
  • 483