当前位置: 首页 > news >正文

多线程中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() {@Overridepublic 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 thenit will be passed up the call stack */}}}private native void start0();
http://www.lryc.cn/news/373053.html

相关文章:

  • Nginx基础理论
  • 【QT5】<应用> 小游戏:贪吃蛇
  • 【Webpack】使用 Webpack 构建 Vue3+TS 项目
  • 数据防泄漏的六个步骤|数据防泄漏软件有哪些
  • SpringCloud 网关Gateway配置并使用
  • MySQl基础----Linux下搭建mysql软件及登录和基本使用(附实操图超简单一看就会)
  • PostgreSQL17优化器改进(4)允许UNION(没有ALL)使用MergeAppend
  • SSM 基于大数据技术的创业推荐系统-计算机毕业设计源码02979
  • 基于WPF技术的换热站智能监控系统03--实现左侧加载动画
  • 4D毫米波雷达技术及发展
  • 请解释Java Web应用的开发流程,包括前后端分离和交互方式。请解释Java中的锁分离技术,并讨论其在提高并发性能方面的作用。
  • selenium使用已经打开的浏览器
  • Redis: 深入解析高性能内存数据库的实现原理
  • 使用 Python进行自动备份文件
  • 02_01_SpringMVC初识
  • Python学习打卡:day04
  • gitlab问题记录
  • OpenCV练习(1)签名修复
  • 软设之系统测试之测试的基本概念及分类
  • Python学习打卡:day06
  • 支付宝 沙盒demo使用
  • ConcurrentHashMap如何保证线程安全?
  • spring属性注入的不细心错误
  • JVM 根可达算法
  • Kafka基础架构与核心概念?有哪些应用场景?
  • 内网不能访问网站怎么办?
  • python-求f(x,n)
  • java值jsp语法笔记
  • 057、PyCharm 运行代码报错:Error Please select a valid Python interpreter
  • Java实现图书管理系统