0

As per my understanding, when i call start() method it will call run method itself.

We can do the same thing by calling run() method . Is it ?

Like we have :

public class ThreadTest extends Thread{

    public static void main(String[] args) {

        ThreadTest t = new ThreadTest();
        t.run();
    }

}

public class ThreadTest extends Thread{

    public static void main(String[] args) {

        ThreadTest t = new ThreadTest();
        t.start();
    }

}

What is the difference between both of them?

Andrew Stubbs
  • 3,887
  • 2
  • 24
  • 45
vermaraj
  • 572
  • 1
  • 6
  • 30
  • Most of these "answers" leave out the most important part. (@immibis came closest though.) Thread.start() is a library method that _starts_ a thread. The run() method is a method that _you_ write, that determines what the thread will do. As immibis said, When your code calls run()---the method that _you_ wrote---that's no different your code calling any other method that you wrote. – Solomon Slow Jul 21 '14 at 13:19

5 Answers5

2

run is just a normal method.

If you do this:

class C {
    public void m() {
        while(true) {}
    }
}
C c = new C();
c.m();
System.out.println("Hello world");

then the infinite loop runs on the current thread, and "Hello world" never gets printed. This is exactly the same as:

class C extends Thread {
    public void run() {
        while(true) {}
    }
}
C c = new C();
c.run();
System.out.println("Hello world");

start is a "special" method which starts a new thread, and then calls run on the new thread.

user253751
  • 45,733
  • 5
  • 44
  • 76
1

The first approach calls the run() method as a simple method call in java.

only one (the calling thread) main thread is running.

But, t.start(); creates a new Thread and calls its run() method.

two threads (main and t runs concurrently)

earthmover
  • 4,039
  • 10
  • 42
  • 73
0

Calling run() directly just executes the code synchronously (in the same thread), just like a normal method call whereas calling start() executes the code asynchronously .

thread.run();

is similar to

thread.start();
thread.join();
Zhenxiao Hao
  • 5,975
  • 7
  • 25
  • 46
0

Calling the run() method directly wont help your cause. It does not spawn a new thread. Rather the run() method will get executed in the calling method stack (same thread).

Documentation of Thread.start() states that it starts a new thread and run() method is invoked internally.

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

Ankur Shanbhag
  • 7,376
  • 2
  • 26
  • 37
0

If you call start() method on Thread object,that will create a new thread which calls run() method internally.
Thread#start

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

If you call run() method then is normal method calling no new thread will be created.Just executes the run() method by current running thread.

Prabhaker A
  • 7,573
  • 1
  • 16
  • 24