What's the difference between Thread start() a

2018-12-31 02:48发布

Say we have these two Runnables:

class R1 implements Runnable {
    public void run() { … }
    …
}

class R2 implements Runnable {
    public void run() { … }
    …
}

Then what's the difference between this:

public static void main() {
    R1 r1 = new R1();
    R2 r2 = new R2();

    r1.run();
    r2.run();
}

And this:

public static void main() {
    R1 r1 = new R1();
    R2 r2 = new R2();
    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);

    t1.start();
    t2.start();
}

14条回答
浪荡孟婆
2楼-- · 2018-12-31 03:03

The difference is that Thread.start() starts a thread that calls the run() method, while Runnable.run() just calls the run() method on the current thread.

查看更多
浮光初槿花落
3楼-- · 2018-12-31 03:07

The difference is that when program calls start() method, a new thread is created and code inside run() is executed in new thread while if you call run() method directly no new thread will be created and code inside run() will execute in the current thread directly.

Another difference between start() and run() in Java thread is that you can not call start() twice. Once started, second start() call will throw IllegalStateException in Java while you can call run() method several times since it's just an ordinary method.

查看更多
流年柔荑漫光年
4楼-- · 2018-12-31 03:07

Actually Thread.start() creates a new thread and have its own execution scenario.

Thread.start() calls the run() method asynchronously,which changes the state of new Thread to Runnable.

But Thread.run() does not create any new thread. Instead it execute the run method in the current running thread synchronously.

If you are using Thread.run() then you are not using the features of multi threading at all.

查看更多
临风纵饮
5楼-- · 2018-12-31 03:08

In the first case you are just invoking the run() method of the r1 and r2 objects.

In the second case you're actually creating 2 new Threads!

start() will call run() at some point!

查看更多
听够珍惜
6楼-- · 2018-12-31 03:08

Start() method call run override method of Thread extended class and Runnable implements interface.

But by calling run() it search for run method but if class implementing Runnable interface then it call run() override method of Runnable.

ex.:

`

public class Main1
{
A a=new A();
B b=new B();
a.run();//This call run() of Thread because run() of Thread only call when class 
        //implements with Runnable not when class extends Thread.
b.run();//This not run anything because no run method found in class B but it 
        //didn't show any error.

a.start();//this call run() of Thread
b.start();//this call run() of Thread
}

class A implements Runnable{
@Override
    public void run() {
            System.out.println("A ");
    }
}

class B extends Thread {

    @Override
    public void run() {
            System.out.println("B ");
    }
}

`

查看更多
何处买醉
7楼-- · 2018-12-31 03:10

If you do run() in main method, the thread of main method will invoke the run method instead of the thread you require to run.

The start() method creates new thread and for which the run() method has to be done

查看更多
登录 后发表回答