什么是JUC

  • 在 Java 中,线程部分是一个重点,本篇文章说的 JUC 也是关于线程的。JUC 就是 java.util .concurrent 工具包的简称。这是一个处理线程的工具包,JDK 1.5 开始出现的

进程与线程

  • 进程(Process) 是计算机中的程序关于某数据集合上的一次运行活动,是系 统进行资源分配和调度的基本单位,是操作系统结构的基础。 在当代面向线程 设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的 描述,进程是程序的实体。是计算机中的程序关于某数据集合上的一次运行活 动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。程序是 指令、数据及其组织形式的描述,进程是程序的实体
  • 线程(thread) 是操作系统能够进行运算调度的最小单位。它被包含在进程之 中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流, 一个进程中可以并发多个线程,每条线程并行执行不同的任务
  • 总结来说:
    • 进程:指在系统中正在运行的一个应用程序;程序一旦运行就是进程;进程— —资源分配的最小单位。
    • 线程:系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个 单元执行流。线程——程序执行的最小单位。

线程的状态

  1. public enum State {
  2. /**
  3. * 新建
  4. * Thread state for a thread which has not yet started.
  5. */
  6. NEW,
  7. /**
  8. * 准备就绪
  9. * Thread state for a runnable thread. A thread in the runnable
  10. * state is executing in the Java virtual machine but it may
  11. * be waiting for other resources from the operating system
  12. * such as processor.
  13. */
  14. RUNNABLE,
  15. /**
  16. * 阻塞
  17. * Thread state for a thread blocked waiting for a monitor lock.
  18. * A thread in the blocked state is waiting for a monitor lock
  19. * to enter a synchronized block/method or
  20. * reenter a synchronized block/method after calling
  21. * {@link Object#wait() Object.wait}.
  22. */
  23. BLOCKED,
  24. /**
  25. * 不见不散
  26. * Thread state for a waiting thread.
  27. * A thread is in the waiting state due to calling one of the
  28. * following methods:
  29. * <ul>
  30. * <li>{@link Object#wait() Object.wait} with no timeout</li>
  31. * <li>{@link #join() Thread.join} with no timeout</li>
  32. * <li>{@link LockSupport#park() LockSupport.park}</li>
  33. * </ul>
  34. *
  35. * <p>A thread in the waiting state is waiting for another thread to
  36. * perform a particular action.
  37. *
  38. * For example, a thread that has called <tt>Object.wait()</tt>
  39. * on an object is waiting for another thread to call
  40. * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
  41. * that object. A thread that has called <tt>Thread.join()</tt>
  42. * is waiting for a specified thread to terminate.
  43. */
  44. WAITING,
  45. /**
  46. * 过期不候
  47. * Thread state for a waiting thread with a specified waiting time.
  48. * A thread is in the timed waiting state due to calling one of
  49. * the following methods with a specified positive waiting time:
  50. * <ul>
  51. * <li>{@link #sleep Thread.sleep}</li>
  52. * <li>{@link Object#wait(long) Object.wait} with timeout</li>
  53. * <li>{@link #join(long) Thread.join} with timeout</li>
  54. * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
  55. * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
  56. * </ul>
  57. */
  58. TIMED_WAITING,
  59. /**
  60. * 终结
  61. * Thread state for a terminated thread.
  62. * The thread has completed execution.
  63. */
  64. TERMINATED;
  65. }
  • wait/sleep 的区别
    • sleep 是 Thread 的静态方法,wait 是 Object 的方法,任何对象实例都 能调用。
    • sleep 不会释放锁,它也不需要占用锁。wait 会释放锁,但调用它的前提 是当前线程占有锁(即代码要在 synchronized 中)。

并发与并行

  • 串行模式
    • 串行是一次只能取得一个任务,并执行这个任务
  • 并行模式
    • 并行意味着可以同时取得多个任务,并同时去执行所取得的这些任务。并行模 式相当于将长长的一条队列,划分成了多条短队列,所以并行缩短了任务队列 的长度。并行的效率从代码层次上强依赖于多进程/多线程代码,从硬件角度上 则依赖于多核 CPU
  • 并发模式
    • 并发(concurrent)指的是多个程序可以同时运行的现象,更细化的是多进程可 以同时运行或者多指令可以同时运行
  • 并发:同一时刻多个线程在访问同一个资源,多个线程对一个点
    • 例子:春运抢票 电商秒杀…
  • 并行:多项工作一起执行,之后再汇总
    • 例子:泡方便面,电水壶烧水,一边撕调料倒入桶中

管程

  • 管程(monitor)是保证了同一时刻只有一个进程在管程内活动,即管程内定义的操作在同 一时刻只被一个进程调用(由编译器实现).但是这样并不能保证进程以设计的顺序执行
  • JVM 中同步是基于进入和退出管程(monitor)对象实现的,每个对象都会有一个管程 (monitor)对象,管程(monitor)会随着 java 对象一同创建和销毁
  • 执行线程首先要持有管程对象,然后才能执行方法,当方法完成之后会释放管程,方 法在执行时候会持有管程,其他线程无法再获取同一个管程

用户线程和守护线程

  • 用户线程:平时用到的普通线程
  • 自定义线程 守护线程:运行在后台,是一种特殊的线程,比如垃圾回收
  • 当主线程结束后,用户线程还在运行,JVM 存活
  • 如果没有用户线程,都是守护线程,JVM 结束