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

Java线程

我为什么要写这篇文章?主要有一下几点:
  • 加深自己对Java线程的理解
  • 勘误网上有些错误的知识和认知
  • 通过写文章,来交流思想
网上关于Java线程的文章数不胜数,实在太多了,我又怎么写一篇让其他人一看就懂,一看就会,一看就忘不掉的文章呢?
  • 用假如我是面试官的场景和面试官的角度去深入分析Java线程相关的知识,下面就开始吧!
NO1.你熟练使用Java线程,那么Java线程有几种状态?
这个时候,大部分同学都应该能回答上来,但是,答案有可能不一样,因为百度上面有五种、六种的很多说法,你说哪个是对的?如下图:
关于Java线程,你必须掌握的知识点
文章图片

这个时候你慌不慌,百度是不可靠的,那么可靠的答案在哪里?在源码里面
Java中Thread的状态(State)
/** * A thread state.A thread can be in one of the following states: *
    *
  • {@link #NEW}
    *A thread that has not yet started is in this state. *
  • *
  • {@link #RUNNABLE}
    *A thread executing in the Java virtual machine is in this state. *
  • *
  • {@link #BLOCKED}
    *A thread that is blocked waiting for a monitor lock *is in this state. *
  • *
  • {@link #WAITING}
    *A thread that is waiting indefinitely for another thread to *perform a particular action is in this state. *
  • *
  • {@link #TIMED_WAITING}
    *A thread that is waiting for another thread to perform an action *for up to a specified waiting time is in this state. *
  • *
  • {@link #TERMINATED}
    *A thread that has exited is in this state. *
  • *
* * * 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. * * @since1.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: *
    *
  • {@link Object#wait() Object.wait} with no timeout
  • *
  • {@link #join() Thread.join} with no timeout
  • *
  • {@link LockSupport#park() LockSupport.park}
  • *
* * A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * 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: *
    *
  • {@link #sleep Thread.sleep}
  • *
  • {@link Object#wait(long) Object.wait} with timeout
  • *
  • {@link #join(long) Thread.join} with timeout
  • *
  • {@link LockSupport#parkNanos LockSupport.parkNanos}
  • *
  • {@link LockSupport#parkUntil LockSupport.parkUntil}
  • *
*/ 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.线程的状态流转,你能讲一下?
我相信很多人都能回答上来,但是,都不是实际的使用经验,而是背下来的。那么,我们来深入的实践一下!
线程状态流转图 关于Java线程,你必须掌握的知识点
文章图片

sleep时候,线程是什么状态?
线程在sleep的时候,是TIMED_WAITING状态或者RUNNABLE状态,决定因素是sleep的时间是0,或者大于0的值:
关于Java线程,你必须掌握的知识点
文章图片

Join的时候,线程是什么状态?
线程在join的时候,是TIMED_WAITING或者WAITING,决定因素是有没有加上时间。
关于Java线程,你必须掌握的知识点
文章图片

Wait时候是什么状态?
线程在wait的时候,是TIMED_WAITING或者WAITING,决定因素是有没有加上时间。
关于Java线程,你必须掌握的知识点
文章图片

抢不到锁的时候,是什么状态?
抢不到锁的时候是BLOCKED状态
关于Java线程,你必须掌握的知识点
文章图片

NO3.线程通信
控制两个线程交换打印(手写) 关于Java线程,你必须掌握的知识点
文章图片

把一副扑克分给4个人(手写) 关于Java线程,你必须掌握的知识点
文章图片

NO4.线程安全与可见性
【关于Java线程,你必须掌握的知识点】安全和可见性,就不展开写了。关注synchronized、volatile,这里面就设计到JMM的知识了。

    推荐阅读