Java中Thread类的start()和run()方法

1、两个方法的区别

start() : 它的作用是启动一个新线程,新线程会执行相应的run()方法。start()不能被重复调用
run()   : run()就和普通的成员方法一样,可以被重复调用。单独调用run()的话,会在当前线程中执行run(),而并不会启动新线程。

举例说明:

class ThreadTest extends Thread{  
    public void run(){
        ...
    } 
};
然后假设在main方法中带创建了一个线程mythread
ThreadTest mythread = new ThreadTest ();

mythread .start()会启动一个新线程,并在新线程中运行run()方法。
而mythread .run()则会直接在当前线程(main方法对应的线程)中运行run()方法,并不会启动一个新线程来运行run()。

2、案例详解

class TestThread extends Thread{
    public TestThread(String name) {
        super(name);
    }

    public void run(){
        System.out.println(Thread.currentThread().getName()+" is running");
    }
};

public class ThreadRunAndStart {
    public static void main(String[] args) {
        TestThread mythread=new TestThread("mythread");

        System.out.println(Thread.currentThread().getName()+" call mythread.run()");
        mythread.run();

        System.out.println(Thread.currentThread().getName()+" call mythread.start()");
        mythread.start();
    }
}

输出结果:

main call mythread.run()
main is running
main call mythread.start()
mythread is running

分析:

(1) Thread.currentThread().getName()是用于获取“当前线程”的名字。当前线程是指正在cpu中调度执行的线程
(2) mythread.run()是在“主线程main”中调用的,该run()方法直接运行在“主线程main”上。
(3) mythread.start()会启动“线程mythread”,“线程mythread”启动之后,会调用run()方法;此时的run()方法是运行在“线程mythread”上。

3、重复调用start方法的结果

如果上述代码中,连续多次执行mythread.start();则会显示如下异常

Exception in thread "main" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:708)
    at thread.ThreadRunAndStart.main(ThreadRunAndStart.java:28)

为什么会出现这种异常呢?查看源码可知,在调用start方法时,先检查线程的状态,如果不是“就绪状态”,也即线程状态值不是0,则抛出异常。Thread.java中start()方法的源码如下:

public synchronized void start() {
    // 如果线程不是"就绪状态",则抛出异常!
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    // 将线程添加到ThreadGroup中
    group.add(this);

    boolean started = false;
    try {
        // 通过start0()启动线程
        start0();
        // 设置started标记
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
        }
    }
}

说明:start()实际上是通过本地方法start0()启动线程的。而start0()会新运行一个线程,新线程会调用run()方法(也即新建一个线程,并且调用线程的run方法)

private native void start0();

Thread.java中run()方法的源码如下:

public void run() {
    if (target != null) {
        target.run();
    }
}

target是一个Runnable对象。run()就是直接调用Thread线程的Runnable成员的run()方法,并不会新建一个线程(所以直接调用run方法,不会创建新的线程,直接在当前线程中调用run方法)

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值