Thread的start()和run()

thread的start方法和run方法有啥不一样

直接看代码,冒泡排序代码不用管,懒得再写一个方法直接用冒泡排序的方法

package com.zhoujin.test;

import java.util.Arrays;

public class Sort {
    public static void bubbleSort(int[] arr){
        int temp;
        //表示趟数,一共arr.length-1次。
        for(int i=0; i<arr.length-1; i++) {
            for (int j = arr.length - 1; j > i; j--) {
                if (arr[j] < arr[j - 1]) {
                    temp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = temp;
                }
            }
        }
        System.out.println("task thread:"+Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        int[] arr={9,7,8,6};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
       Runnable runnable= () -> bubbleSort(arr);
       Thread thread=new Thread(runnable);
       thread.run();
//       thread.start();
        System.out.println("main:"+Thread.currentThread().getName());
    }

}

1)当使用run()方法启用线程时

2)当使用start()方法启用线程时

可以发现当我们调用run()方法时,执行bubbleSort()方法的线程的事同一个线程即主线程,当我们调用start()方法时,执行bubbleSort()方法的线程为Thread-0,我们可以打开Thread的源码,查看这两个方法有何不同,首先看Thread的start()方法

    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            //注意这个地方调用了start0这个方法
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }
    //这是一个java的JVM底层实现的原生方法,具体去看jdk的源码吧
    private native void start0();

我们再看看Thread的run()方法

/**
     * If this thread was constructed using a separate
     * <code>Runnable</code> run object, then that
     * <code>Runnable</code> object's <code>run</code> method is called;
     * otherwise, this method does nothing and returns.
     * <p>
     * Subclasses of <code>Thread</code> should override this method.
     *
     * @see     #start()
     * @see     #stop()
     * @see     #Thread(ThreadGroup, Runnable, String)
     */
    @Override
    public void run() {
        if (target != null) {
            //target就是Runnable
            target.run();
        }
    }

总结:

1.start()方法来启动线程,真正实现了多线程运行。这时无需等待run方法体代码执行完毕,可以直接继续执行下面的代码;通过调用Thread类的start()方法来启动一个线程, 这时此线程是处于就绪状态, 并没有运行。 然后通过此Thread类调用方法run()来完成其运行操作的, 这里方法run()称为线程体,它包含了要执行的这个线程的内容, Run方法运行结束, 此线程终止。然后CPU再调度其它线程。
2.run()方法当作普通方法的方式调用。程序还是要顺序执行,要等待run方法体执行完毕后,才可继续执行下面的代码; 程序中只有主线程——这一个线程, 其程序执行路径还是只有一条, 这样就没有达到写线程的目的。
记住:多线程就是分时利用CPU,宏观上让所有线程一起执行 ,也叫并发

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值