Java multi-threaded programming foundation 1 (basic thread mechanism, thread scheduler, thread state)

Article citations

1 Java Programming Ideas Chapter 21 Basic Threading Mechanism p653

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

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


First, the basic thread mechanism

Concurrent programming allows us to divide a program into separate, independently running tasks. By using multithreading mechanism, each of these independent tasks (also called subtasks) will be driven by a thread of execution. A thread is a single sequential flow of control within a process, so a single process can have multiple tasks executing concurrently, but your program makes each task appear to have its own CPU. The underlying mechanism is slicing CPU time, but you don't need to think about it.

The threading model brings convenience, which simplifies the processing of multiple operations that are simultaneously intertwined in a single program. When using threads, the CPU will in turn allocate its occupied time to each task (this is indeed the case when the system uses a time-slicing mechanism. Solaris uses a FIFO concurrency model: unless a high-priority thread is woken up, the current thread will run until the current thread will run until it is blocked or terminated. This means that other threads with the same priority will not run until the current thread relinquishes the processor). Each task feels like it's using the CPU all the time, but in fact the CPU time is divided into segments and distributed to all tasks (exception is that the program does run on multiple CPUs). One of the great things about threads is that it takes you out of this hierarchy, even if the code doesn't have to know if it's running on a machine with one or more CPUs. So, using threading is a way to build transparent, scalable programs. If the program runs too slowly, adding a CPU to the machine can easily speed up the program. Multitasking and multithreading are often the most logical ways to use a multiprocessing system.

Second, the thread scheduler

from Baidu Encyclopedia

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

The core of the operating system, which is actually a resident memory program, continuously scans the thread queue, and uses specific algorithms (time slice rotation method, priority scheduling method, multi-level feedback queue scheduling method, etc.) The thread that occupies the CPU has more CPU usage rights, and reclaims the processor from the previous thread, and then makes the thread to be run occupy the processor.

The above description is very simple, although very abstract, but at least one can imagine what role the thread scheduler plays.

A brief description of the thread scheduler can also refer to

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

Any Baidu answer

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

The thread scheduler is an operating system service that is responsible for allocating CPU time to threads in the Runnable state. Once a thread is created and started, its execution depends on the thread scheduler implementation. Time slicing refers to the process of allocating available CPU time to available Runnable threads. Allocating CPU time can be based on thread priority or how long a thread waits. Thread scheduling is not under the control of the Java virtual machine, so it is better to let the application control it (ie don't make the program depend on the priority of the thread).

There are a few things I don't agree with above, but it seems to be the case.

3. Thread status

Look at the status of the thread through the Thread class source code

 /**
     * 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;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324776748&siteId=291194637