学习总结录 学习总结录
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
  • Java基础

  • Java集合

  • MySQL

  • Redis

  • JVM

  • 多线程

    • Java多线程-创建线程
    • Java多线程-启动线程
      • Java多线程02-启动线程
        • start()方法原理
        • run()方法原理
        • 小结
    • Java多线程-停止中断线程
    • Java多线程-生命周期
    • Java多线程-Thread和Object
    • Java多线程-线程属性
    • Java多线程-线程异常
    • Java多线程-线程安全
    • Java多线程-死锁问题和解决方案
  • 计算机网络

  • Spring

  • Kafka

  • Elasticsearch

  • Python

  • 面试专题

  • 知识库
  • 多线程
旭日
2023-03-31
目录
Java多线程02-启动线程
start()方法原理
run()方法原理
小结

Java多线程-启动线程

# Java多线程02-启动线程

java启动线程有两个方法:

  • start()
  • run()
public class StartAndRunMethod {
    public static void main(String[] args) {
        Runnable runnable = () -> {
            System.out.println(Thread.currentThread().getName());
        };
        runnable.run();

        new Thread(runnable).start();
    }
}

# start()方法原理

start方法就是去启动新线程,执行流程如下:

  • 检查线程状态,只有NEW状态下的线程才能继续,否则会抛出IllegalThreadStateException异常。
  • 加入到线程组。
  • 调用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;
        } 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 */
            }
        }
    }

# run()方法原理

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

run()方法会根据传入的对象执行不同结果

# 小结

start()才是真正启动一个线程,而如果直接调用run(),那么run只是一个普通的方法而已,和线程的生命周期没有任何关系。

#Java
上次更新: 2024/06/29, 15:13:44
Java多线程-创建线程
Java多线程-停止中断线程

← Java多线程-创建线程 Java多线程-停止中断线程→

最近更新
01
基础概念
10-31
02
Pytorch
10-30
03
Numpy
10-30
更多文章>
Theme by Vdoing | Copyright © 2021-2025 旭日 | 蜀ICP备2021000788号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式