浅析Thread和线程的关系

Thread是Java中的类,它描述了线程,它本身并不是线程。线程和Thread是两个不同纬度的东西,不要混为一谈。(具体可以这样想:线程是运行的CPU,Thread对象只是内存中的数据)

常常研究各种开源框架与技术,不知道大家有没有看过我们常用的Thread类,虽然是一个很基础的类,但是有很多本质的东西值得我们去学习,从而能提高我们日常编码的心得。(类似还有Integer、String等等,你们看了吗,可能我们认为很简单,其实并非如此)

我们先简单介绍一下Thread类,然后再去分析它和线程的关系。

Thread类介绍

Java中的Thread类是线程的抽象表示,它定义了线程的基本行为和属性,并不是线程本身。

对于老生常谈的Thread状态转换什么的就不说了,下面主要聊一聊涉及本文的重要方法。

Thread创建

Thread的构造方法最终都会调用init方法执行,我们直接看init

/**
 * Initializes a Thread.
 *
 * @param g the Thread group
 * @param target the object whose run() method gets called
 * @param name the name of the new Thread
 * @param stackSize the desired stack size for the new thread, or
 *        zero to indicate that this parameter is to be ignored.
 * @param acc the AccessControlContext to inherit, or
 *            AccessController.getContext() if null
 * @param inheritThreadLocals if {@code true}, inherit initial values for
 *            inheritable thread-locals from the constructing thread
 */
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();
}

大致看一下这一段代码,其实也就是给当前Thread对象的一些属性进行了初始化,并没有做其他的。

start方法

/**
 * Causes this thread to begin execution; the Java Virtual Machine
 * calls the <code>run</code> method of this thread.
 * <p>
 * The result is that two threads are running concurrently: the
 * current thread (which returns from the call to the
 * <code>start</code> method) and the other thread (which executes its
 * <code>run</code> method).
 * <p>
 * It is never legal to start a thread more than once.
 * In particular, a thread may not be restarted once it has completed
 * execution.
 * 
 * 使此线程开始执行;Java虚拟机调用该线程的run方法。
 * 结果是两个线程同时运行:当前线程(从对启动方法的调用返回)和另一个线程(执行其运行方法)。
 *
 * @exception  IllegalThreadStateException  if the thread was already
 *               started.
 * @see        #run()
 * @see        #stop()
 */
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 */
        }
    }
}
// 运行jvm的本地方法启动操作系统线程
private native void start0();

从run方法的解释我们可以看到运行过start方法后,结果是两个线程同时运行:当前线程(从对启动方法的调用返回)和另一个线程(执行其运行方法)。这里才真正启动了一个新的线程,这个新的线程可以通过“Thread对象进行操作”。

currentThread方法

/**
 * Returns a reference to the currently executing thread object.
 *
 * @return  the currently executing thread.
 */
public static native Thread currentThread();

仍然是native方法,这里获取的当前线程,是执行run方法的线程,下面来看run方法。

重要的run方法

/**
 * If this thread was constructed using a separate
 * <code>Runnable</code> run object, then that
 * <code>Runnable</code> object's <code>run</code> method is called;
 * otherwise, this method does nothing and returns.
 * <p>
 * Subclasses of <code>Thread</code> should override this method.
 *
 * @see     #start()
 * @see     #stop()
 * @see     #Thread(ThreadGroup, Runnable, String)
 */
@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

这个方法就是线程会运行到的代码块,也是线程运行的入口,线程运行的代码内容都由他展开。
你会看到这个方法是没有地方调用的,但是start过后就会运行run方法,是因为JVM虚拟机底层的C++代码会去调用这里的run的。

小结

【记住】:Thread本身只是一个类,Thread对象会去对应一个Java线程,它定义了线程的基本行为和属性,通过Thread对象,其他人可以去从操控线程。

线程介绍

什么是线程
线程=进程-共享资源

线程的优点

  • 一个进程可以同时存在多线程
  • 各个线程之间可以并发执行。
  • 各个线程之间可以共用地址空间和文件等资源

线程的缺点

  • 一个线程崩溃,会导致其所属进程的所有线程崩溃

线程和进程比较

  • 进程是资源分配单位,线程是CPU调度单位。
  • 进程拥有一个完成的资源平台,而线程值独享必不可少的资源,如寄存器和栈
  • 线程同样具有就绪、阻塞和执行三种基本状体啊,同样具有状态之间的转换关系
  • 线程能减少并发执行的时间和空间开销
    • 线程的创建时间比进程短
    • 线程的终止时间比进程短
    • 同一进程内的线程切换时间比进程短
    • 由于同一进程的各线程间共享内存和文件资源,可直接进行不通过内核的通信

Thread与线程的关系

  1. 线程只是执行流程,并不是Thread对象,它俩是不同纬度的存在。线程可以理解为CPU运行,Thread对象可以理解为内存中的数据。
  2. new Thread()只代表新建了一个对象,执行start才会进行系统调用创建一个线程,Thread描述了线程。
  3. 线程运行的入口是Thread的run方法,线程的执行流程由此展开。
  4. main方法准确的来说是启动了一个java进程,只不过一开始只有一个线程(执行流程)在执行(我们关注到的线程只有一个,其实一个Java进程启动,内部还有很多其他的线程)。
  5. 每一个线程都会持有一个Thread对象的引用,Thread对象本身是存储在堆上的,不属于任何一个线程
  6. 如果当前线程执行到某一个方法内,那么堆中必然存在此方法所在类的实例(静态方法除外),那么也就会持有该类的this引用。
  7. 线程在执行过程中,如果存在多个对象调用,如果调用链路进入下层对象中,并且不会再进入上层对象,同时也用不到上层对象,那么上层对象的生命周期结束,可以被GC。
  8. 代码是静态的存储在代码区中(必然存在于进程中),进程动态运行的原因是因为线程的存在。
  9. 线程能运行的原因是该线程获取了CPU时间片。
  • 25
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倜傥村的少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值