关于Java线程,你必须掌握的知识点

Java线程

我为什么要写这篇文章?主要有一下几点:

  • 加深自己对Java线程的理解
  • 勘误网上有些错误的知识和认知
  • 通过写文章,来交流思想

网上关于Java线程的文章数不胜数,实在太多了,我又怎么写一篇让其他人一看就懂,一看就会,一看就忘不掉的文章呢?

  • 用假如我是面试官的场景和面试官的角度去深入分析Java线程相关的知识,下面就开始吧!

NO1.你熟练使用Java线程,那么Java线程有几种状态?

这个时候,大部分同学都应该能回答上来,但是,答案有可能不一样,因为百度上面有五种、六种的很多说法,你说哪个是对的?如下图:
在这里插入图片描述
这个时候你慌不慌,百度是不可靠的,那么可靠的答案在哪里?在源码里面

Java中Thread的状态(State)

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

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

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

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

        /**
         * 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 state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

java中线程的状态有6中,其中包括:NEW(新建),RUNNABLE(运行,可能抢到了CPU,也可能没有抢到),BLOCKED(阻塞),WAITING(等待),TIMED_WAITING(加上时间的等待),TERMINATED(销毁)。

NO2.线程的状态流转,你能讲一下?

我相信很多人都能回答上来,但是,都不是实际的使用经验,而是背下来的。那么,我们来深入的实践一下!

线程状态流转图

在这里插入图片描述

sleep时候,线程是什么状态?

线程在sleep的时候,是TIMED_WAITING状态或者RUNNABLE状态,决定因素是sleep的时间是0,或者大于0的值:
在这里插入图片描述

Join的时候,线程是什么状态?

线程在join的时候,是TIMED_WAITING或者WAITING,决定因素是有没有加上时间。
在这里插入图片描述

Wait时候是什么状态?

线程在wait的时候,是TIMED_WAITING或者WAITING,决定因素是有没有加上时间。
在这里插入图片描述

抢不到锁的时候,是什么状态?

抢不到锁的时候是BLOCKED状态
在这里插入图片描述

NO3.线程通信

控制两个线程交换打印(手写)

在这里插入图片描述

把一副扑克分给4个人(手写)

在这里插入图片描述

NO4.线程安全与可见性

安全和可见性,就不展开写了。关注synchronized、volatile,这里面就设计到JMM的知识了。

发布了78 篇原创文章 · 获赞 29 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/lvhonglei1987/article/details/104592638
今日推荐