Java Multithreaded Programming Foundation 1 (Basic Thread Mechanism, Thread Scheduler, Thread State)

Posted by jarow on Thu, 16 May 2019 08:40:46 +0200

Article Citation

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

1. Basic threading mechanisms

Concurrent programming allows us to say that programs are divided into separate, independent tasks.By using a multithreading mechanism, each of these independent tasks (also known as subtasks) will be driven by the execution thread.A thread is a single sequential control flow in a process, so a single process can have multiple tasks executed concurrently, but your program makes each task appear to have its own CPU.The underlying mechanism is to slice CPU time, but you don't need to consider it.

The threading model facilitates change by simplifying the processing of multiple operations that are interwoven together simultaneously in a single program.When using threads, the CPU will alternately allocate its elapsed time to Task 2 (this is true when the system uses the time slicing mechanism).Solaris uses the FIFO concurrency model: unless a high-priority thread is awakened, 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 discards the processor.Each task feels like it's occupying the CPU all the time, but in fact the CPU time is fragmented and assigned to all tasks (with the exception that the program does run on more than one CPU).One benefit of threading is that it allows you to pull out of this level, even if the code doesn't have to know if it's running on a machine with one or more CPUs.So using the threading mechanism is a way to build transparent, scalable programs. If programs run too slowly, adding a CPU to the machine can easily speed up the program.Multitask and multithreading are often the most appropriate ways to use a multiprocessing system.

2. 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 is actually a resident memory program that continuously scans the thread queue, uses specific algorithms (time slice rotation, priority scheduling, multi-level feedback queue scheduling, etc.) to find threads that have more CPU usage than threads currently holding the CPU, retrieves the processor from previous threads, and makes the threads to be run occupy processingApparatus.

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

A simple description of the thread scheduler can also be referenced

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

Answer freely from Baidu

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

Thread Scheduler is an operating system service that is responsible for allocating CPU time to threads in a Runnable state.Once a thread is created and started, its execution depends on the implementation of the thread scheduler.Time slicing is the process of allocating available CPU time to available Runable threads.Allocating CPU time can be based on thread priority or the time a thread waits.Thread scheduling is not controlled by the Java virtual machine, so it is a better choice for applications to control it (that is, not to let programs depend on thread priority).

I don't agree with some of the above, but it looks like this.

3. Thread State

See the state of threads from the Thread class source

 /**
     * 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 {

        /**
         * A thread just created to start is in this state.Since a thread instance can only be started once.So a thread may only be in this state again
         */
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * The state can be viewed as a conforming state.It includes two sub-states: READY and RUNNING.The former identifies that threads in this state can be used by the JVM
         * The thread dispatcher (Schedule) is dispatched to a Running state.The latter indicates that the thread in that state is running, that is, responding to the thread object's
         * run The instructions for the code in the method are being executed by the CPU.When the yield method of the Thread instance is called or because of the thread scheduler,
         * The state of the corresponding thread is swapped from RUNNING to 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,

        /**
         * A thread that initiates a Blocking I/O operation or attempts to acquire a lock held by another thread
         * Will be in this state.Threads in this state do not consume CPU resources.When the corresponding lock is released by another thread, the state of that thread can be converted to
         * 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,

        /**
         * A thread that makes certain method calls is in this state of waiting indefinitely for other threads to perform specific operations.These methods include:
         * Object.wait()\Thread.join()\LockSupport.park(). Appropriate method to convert the corresponding thread from WAITING to RUNNING
         * Includes: 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,

        /**
         * This state is similar to WAITING, except that the thread in this state is not infinitely waiting for other threads to perform specific operations, but is time-bound
         * Waiting state.The state of the thread automatically switches to RUNNABLE when other threads do not perform the specific operation expected by the thread within the specified time.
         */
        /**
         * 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,

        /**
         * The thread whose execution has ended is in this state.Since an instance of a thread can only be started once, a thread can only be in that state again.
         * Thread The normal return of an instance's run method or premature termination due to an exception thrown will leave the corresponding thread in that state.
         */
        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

Topics: Java Programming jvm