首页 > 编程语言 >start线程开启(C源码分析)

start线程开启(C源码分析)

时间:2023-03-27 11:22:08浏览次数:39  
标签:group Thread start0 start 源码 线程 jvm

一个线程开启都经历了什么

public class ThreadBaseDemo {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
        }, "t1");
        t1.start();
    }
}
  • start 源码

    start 被 synchronized 修饰,必须全部执行完

    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 */
            }
        }
    }
    
  • start0 方法

    java 线程是通过 start 的方法启动执行的,主要内容在 native 方法 start0 中,openjdk 的写 JNl^(Java Native Interface,Java本地接口)^ 一般是一一对应的,Thread.java 对应的就是 Thread.c

    start0 其实就是 JVM_StartThread。此时查看源代码可以看到在 jvm.h 中找到了声明,jvm.cpp 中有实现。

    private native void start0();
    

    start0 的执行流程一般如下所示

    image

    • thread.c 部分

      image

    • jvm.cpp

      image
      image

    • thread.cpp

      image

    从上面三部分内容来看,一个线程的启动是由 jvm 配合操作系统,底层由操作系统来分配一个原生的基础线程

标签:group,Thread,start0,start,源码,线程,jvm
From: https://www.cnblogs.com/ShaunY/p/17260935.html

相关文章

  • 太坑了,我竟然从RocketMQ源码中扒出了7种导致消息重复消费的原因
    大家好,我是三友~~在众多关于MQ的面试八股文中有这么一道题,“如何保证MQ消息消费的幂等性”。为什么需要保证幂等性呢?是因为消息会重复消费。为什么消息会重复消费?明明......
  • lucene4.5源码分析系列:索引缓存以及刷新
    缓存和刷新是比较重要的问题,它涉及到lucene如何管理内存和磁盘。前面提到索引的结果是缓存在内存中的,等到一定时候才会将其刷新到硬盘上去。缓存在这里的目的无非是缓解高速......
  • Spring源码核心剖析
    作者:京东科技韩国凯前言SpringAOP作为Spring最核心的能力之一,其重要性不言而喻。然后需要知道的是AOP并不只是Spring特有的功能,而是一种思想,一种通用的功能。而SpringAOP只......
  • Spring源码核心剖析
    作者:京东科技韩国凯前言SpringAOP作为Spring最核心的能力之一,其重要性不言而喻。然后需要知道的是AOP并不只是Spring特有的功能,而是一种思想,一种通用的功能。而SpringAO......
  • 多线程的同步和互斥—线程的信号量
    同步://account.h#ifndef_ACCOUNT_H#define_ACCOUNT_H#include<pthread.h>#include<semaphore.h>typedefstruct{intcode;doublebalance;......
  • Python多任务-多线程-多进程-协程-进阶学习
    --多任务-多线程-多进程-协程-进阶学习--文中所提到的案例参考:GITHUB中项目文件夹https://github.com/FangbaiZhang/Python_advanced_learning/tree/master/02_Python_ad......
  • Task 类 多线程
    Task类定义命名空间: System.Threading.Tasks程序集:System.Runtime.dll表示一个异步操作publicclassTask:IAsyncResult,IDisposable继承  Object->Task......
  • 线程(确实还有没理解到位的地方)
    多线程Thread类多条执行路径,主线程和子线程并行交替执行packagexiancheng;publicclassDemo01extendsThread{//创建线程方式一:继承Thread类,重写run方法,调用s......
  • HashTable源码分析
    HashTable是一个线程安全的HashMap,是jdk早期版本的产物,但其效率较低1.初始化可以看到,与HashMap不同,HashTable无参构造是默认会构造一个容量为11的数组,而HashMap在无参......
  • 多线程的互斥—读写锁
    //account.h#ifndef_ACCOUNT_H#define_ACCOUNT_H#include<pthread.h>typedefstruct{intcode;doublebalance;//定义一把互斥锁,用......