Java多线程编程基础一(基本线程机制、线程调度器、线程状态)

文章引用

1 java编程思想 21章 基本的线程机制 p653

2 http://www.jb51.net/article/105487.htm

3 https://zhidao.baidu.com/question/1542633956525518747.html


一、基本的线程机制

并发编程使我们可以讲程序划分为多个分离的、独立运行的任务。通过使用多线程机制,这些独立任务(也被称为子任务)中的每一个都将由执行线程来驱动。一个线程就是在进程中的一个单一的顺序控制流,因此,单个进程可以拥有多个并发执行的任务,但是你的程序使得每个任务都好像有自己的CPU一样。其底层机制是切分CPU时间,但是你不需要考虑它。

线程模型为变成带来了便利,它简化了在单一程序中同时交织在一起的多个操作的处理。在使用线程时,CPU将轮流给每个任务二分配其占用时间(当系统使用时间切片机制时,情况确实如此。Solaris使用了FIFO并发模型:除非有高优先级的线程被唤醒,否则当前线程将一直运行,直至当前线程将一直运行,直到它被阻塞或者终止。这意味着具有相同优先级的其他线程在当前线程放弃处理器之前,将不会运行)。每个任务都觉得自己在一直占用CPU,但是事实上CPU时间是划分成片段分配给所有的任务(例外情况是程序确实运行在多个CPU之上)。线程的一大好处是可以使你从这个层次抽身出来,即使代码不必知道它是运行在具有一个还是多个CPU的机器上。所以,使用线程机制是一种建立透明的,可扩展的程序的方法,如果程序运行的太慢,为机器增添一个CPU就能很容易的加快程序的运行速度。多任务和多线程往往是使用多处理系统的最合理方式。

二、线程调度器

来自百度百科的

https://baike.baidu.com/item/%E7%BA%BF%E7%A8%8B%E8%B0%83%E5%BA%A6%E5%99%A8/2154102

操作系统的核心,它实际就是一个常驻内存的程序,不断地对线程队列进行扫描,利用特定的算法(时间片轮转法、优先级调度法、多级反馈队列调度法等)找出比当前占有CPU的线程更有CPU使用权的线程,并从之前的线程中收回处理器,再使待运行的线程占用处理器。

上述描述很简单,虽然很抽象但至少大概能想象一下线程调度器充当什么角色。

线程调度器的简单描述也可以参照

https://blog.csdn.net/jjavaboy/article/details/43340629

随便百度答案

https://zhidao.baidu.com/question/1542633956525518747.html

线程调度器是一个操作系统服务,它负责为Runnable状态的线程分配CPU时间。一旦创建一个线程并启动它,它的执行便依赖于线程调度器的实现。时间分片是指将可用的CPU时间分配给可用的Runnable线程的过程。分配CPU时间可以基于线程优先级或者线程等待的时间。线程调度并不受到Java虚拟机控制,所以由应用程序来控制它是更好的选择(也就是说不要让的程序依赖于线程的优先级)。

上述有一些东西我并不赞同,但是看起来好像是这么回事。

三、线程状态

通过Thread类源码来看一下线程的状态

 /**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    public enum State {

        /**
         * 一个刚创建而为启动的线程处于该状态。由于一个线程实例只能够被启动一次。因此一个线程只可能又一次处于该状态
         */
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * 该状态可以看成是一个符合的状态。它包括两个子状态:READY和RUNNING。前者标识处于该状态的线程可以被JVM的
         * 线程调度器(Schedule)进行调度而使之处于Running状态。后者表示处于该状态的线程正在运行,即响应线程对象的
         * run方法中的代码所对应的指令正在由CPU执行。当Thread实例的yield方法被调用时或者由于线程调度器的原因,
         * 相应线程的状态会由RUNNING装换为READY。
         */
        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

        /**
         * 一个线程发起一个阻塞时I/O(Blocking I/O)操作后,或者试图去获得一个由其他线程持有的锁时,相应的线程
         * 会处于该状态。处于该状态的线程并不会占用CPU资源。当相应的锁被其他线程释放后,该线程的状态又可以转换为
         * RUNNABLE.
         */
        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * 一个线程执行了某些方法调用之后就会处于这种无限等待其他线程执行特定操作的状态。这些方法包括:
         * Object.wait()\Thread.join()\LockSupport.park()。能够使相应的线程从WAITING转换为RUNNING的相应方法
         * 包括:Object.notify()\Object.notifyAll()\LockSupport.unpark(thread).
         */
        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * 该状态和WAITING类似,差别在于处于该状态的线程并非无限的等待其他线程执行特定操作,而是处于带有时间限制的
         * 等待状态。当其他线程没有在指定时间内执行该线程所期望的特定操作时,该线程的状态自动切换为RUNNABLE。
         */
        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * 已经执行结束的线程处于该状态。由于一个线程实例只能够被启动一次,因此一个线程也只可能又一次处于该状态。
         * Thread实例的run方法正常返回或者由于抛出异常而提前终止都会导致相应线程处于该状态。
         */
        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

猜你喜欢

转载自blog.csdn.net/u013412772/article/details/80013856