线程Thread源代码思想学习1

发布时间:2023年12月18日

1.启动线程代码

public class MultiThreadExample {
    public static void main(String[] args) {
        // 创建两个线程对象
        Thread thread1 = new Thread(new Task());
        Thread thread2 = new Thread(new Task());
        // 启动线程
        thread1.start();
        thread2.start();

    }
}

class Task implements Runnable {
    @Override
    public void run() {
        // 执行具体的任务逻辑
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread: " + Thread.currentThread().getId() + " Count: " + i);
            try {
                Thread.sleep(1000); // 模拟耗时操作
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2.解析new Thread(new Task());

调用Thread
下面方法是一个公开的,其他方法都是私有的,保护了线程类方法内部的安全

   public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
    }

调用init,方法变私有 private

   /**
     * Initializes a Thread with the current AccessControlContext.
     * @see #init(ThreadGroup,Runnable,String,long,AccessControlContext,boolean)
     */
    private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize) {
        init(g, target, name, stackSize, null, true);
    }

调用init实际实现private,并给成员变量赋值

private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;

        Thread parent = currentThread();
        SecurityManager security = System.getSecurityManager();
        if (g == null) {
            /* Determine if it's an applet or not */

            /* If there is a security manager, ask the security manager
               what to do. */
            if (security != null) {
                g = security.getThreadGroup();
            }

            /* If the security doesn't have a strong opinion of the matter
               use the parent thread group. */
            if (g == null) {
                g = parent.getThreadGroup();
            }
        }

        /* checkAccess regardless of whether or not threadgroup is
           explicitly passed in. */
        g.checkAccess();

        /*
         * Do we have the required permissions?
         */
        if (security != null) {
            if (isCCLOverridden(getClass())) {
                security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
            }
        }

        g.addUnstarted();

        this.group = g;
        this.daemon = parent.isDaemon();
        this.priority = parent.getPriority();
        if (security == null || isCCLOverridden(parent.getClass()))
            this.contextClassLoader = parent.getContextClassLoader();
        else
            this.contextClassLoader = parent.contextClassLoader;
        this.inheritedAccessControlContext =
                acc != null ? acc : AccessController.getContext();
        this.target = target;
        setPriority(priority);
        if (inheritThreadLocals && parent.inheritableThreadLocals != null)
            this.inheritableThreadLocals =
                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
        /* Stash the specified stack size in case the VM cares */
        this.stackSize = stackSize;

        /* Set thread ID */
        tid = nextThreadID();
    }

3. 解析thread1.start();

成员变量group增加当前实例,并调用本地方法使用虚拟机启动线程

 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是成员变量 能加入线程对象数组内 
        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 */
            }
        }
    }

启动

    private native void start0();

private native void start0(); 是 Java 中的一个本地方法声明。

本地方法是一种在 Java 代码中声明但实际实现却在本地语言(如C或C++)中的方法。这通常用于与操作系统、硬件或其他底层资源进行交互的情况,因为本地语言可以更高效地访问底层资源。

在这个特定的声明中,start0() 方法是一个私有(private)本地方法,没有方法体。通过 native 关键字表示该方法的实现是在本地语言中完成的。

start0() 方法通常是与 Thread.start() 方法相关联的,用于启动线程的执行。在 Thread 类的实现中,start0() 方法的实际实现通常由底层虚拟机(JVM)提供,而不是在 Java 代码中定义。

因为 start0() 是一个本地方法,所以它的具体实现是由底层虚拟机提供的,并且无法在 Java 代码中直接查看或修改其实现。

4. this.target = target;

target 是定义的成员变量,类型为接口
private Runnable target;
this.target = target;作用是将实现此接口的类指向此接口,调用时会调用此接口的实现类

5.学习到什么

1.线程类通过公开方法或者构造方法为对象内部私有变量赋值,保护对象内部成员变量的安全。
2.线程启动会调用之前为成员变量赋值的变量使用

文章来源:https://blog.csdn.net/beiback/article/details/134941286
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。