多线程中run()和start()的区别

我们知道,在多线程中
Thread thread = new Thread(runnable);
thread.start();以及 thread.run();都可以执行runnable中run方法下的代码,但是二者又有所不同
下面给出一段代码用以体现二者的区别:
以下代码中,通过thread.start()启动线程,最终产生了线程阻塞

package com.xuecheng;

/**
 * @author Zonda
 * @version 1.0
 * @description TODO
 * @2024/6/15 16:23
 */
public class ThreadLocal {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                synchronized (this){
                    while(true){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.println("打了一发");
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Thread thread2 = new Thread(runnable);
        thread2.start();
    }
}

但如果是调用run方法区启动就不会,这是为什么呢?

        Thread thread = new Thread(runnable);
        thread.run();
        Thread thread2 = new Thread(runnable);
        thread2.run();

因为当我们直接调用run方法执行的时候,这是直接在main方法的主线程中调用run方法,并没有开启一个新的线程,因此 thread.run();和 thread2.run();会在main方法的主线程中顺序执行。这样就不会出现两个线程去争抢同一个锁中的资源的情况。
在这里插入图片描述
而执行start方法会在main线程中异步地开启一个新线程去执行run方法中的代码,如果有两个线程执行start方法,就会出现两个线程同时去执行run方法中的情况。如果一个其中一个线程休眠的时候另一个线程访问这个方法还好,可以交替访问;但是一旦出现一个线程在执行run方法的时候,另一个线程也同时要执行run方法,但是synchronized关键字中的元素只能被一个线程访问,最终会卡死。
在这里插入图片描述
我们通过阅读源码也可以看出只有在调用start方法的时候才会创建线程:
start0();

    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();
            started = true;//start0();执行成功,走到这里说明线程创建成功
        } 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 */
            }
        }
    }

    private native void start0();

相关推荐

  1. springboot线问题优化处理

    2024-06-16 17:52:02       32 阅读
  2. 进程线区别

    2024-06-16 17:52:02       43 阅读
  3. C# 异步线区别

    2024-06-16 17:52:02       37 阅读
  4. 线进程区别

    2024-06-16 17:52:02       38 阅读
  5. 线进程区别(面试)

    2024-06-16 17:52:02       41 阅读
  6. Boost:asioio_service,线run

    2024-06-16 17:52:02       48 阅读
  7. 使用RunnableCallable接口实现线区别

    2024-06-16 17:52:02       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 17:52:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 17:52:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 17:52:02       82 阅读
  4. Python语言-面向对象

    2024-06-16 17:52:02       91 阅读

热门阅读

  1. 学习分享-FutureTask

    2024-06-16 17:52:02       31 阅读
  2. 基于深度学习的物体材质预测

    2024-06-16 17:52:02       31 阅读
  3. iOS cell的复用以及自定义cell

    2024-06-16 17:52:02       39 阅读
  4. lwip中server和client的socket、地址和端口号

    2024-06-16 17:52:02       35 阅读
  5. DOM的概念?获取html元素的方法有哪些?

    2024-06-16 17:52:02       25 阅读
  6. 深入浅出Python爬虫:掌握数据抓取的艺术

    2024-06-16 17:52:02       24 阅读
  7. lower_bound 和 upper_bound

    2024-06-16 17:52:02       34 阅读
  8. UOS常用命令

    2024-06-16 17:52:02       24 阅读
  9. Spring Boot 增删改查(mybatis-plus)

    2024-06-16 17:52:02       32 阅读
  10. Vue中双向数据绑定是如何实现的

    2024-06-16 17:52:02       28 阅读
  11. dev c++ “permission denied“解决方法

    2024-06-16 17:52:02       34 阅读
  12. 每天一个项目管理概念之敏捷项目管理

    2024-06-16 17:52:02       31 阅读
  13. MongoDB入门与实践

    2024-06-16 17:52:02       28 阅读
  14. 了解protoStuff

    2024-06-16 17:52:02       33 阅读
  15. 计算机网络期末复习

    2024-06-16 17:52:02       54 阅读