JUC概述

JUC概述1:

首先是進程和執行緒的概念:

進程:是指系統在系統中正在運行的一個應用程式,程式一旦運行就是進程,進程是資源分配的最小單位

執行緒:進程之內獨立執行,是程式執行的最小單位

執行緒的六大狀態:在執行緒的枚舉類中

 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 {@code Object.wait()}
          * on an object is waiting for another thread to call
          * {@code Object.notify()} or {@code Object.notifyAll()} on
          * that object. A thread that has called {@code 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:
          * <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;
     }
狀態名稱 說明
new 初始狀態
runnable 運行狀態
blocked 阻塞狀態
waiting 等待狀態,一直等(不見不散)
time_waiting 超時等待,(過時不候)
terminated 終止狀態

wait和sleep的區別:

  1. sleep是Thread的靜態方法,wait是Object的方法,任何對象實例化都能調用
  2. sleep不會釋放鎖,他也不需要佔用鎖,wait會釋放鎖,但是調用它的前提是當前執行緒佔有鎖
  3. 它們都可以interrupted被中斷

並發和並行:

並發是指多個事情在同一個時間段中執行

並行是指多個事情在同一時刻執行

管程:

是一種同步機制,保證同一時間內只有一個執行緒訪問被保護數據或者程式碼

jvm同步基於進入(加鎖)和退出(解鎖),是管程對象實現的

大意就是進加鎖,退是解鎖,通過管程對象管理

用戶執行緒:自定義執行緒 主執行緒結束了,用戶執行緒還存在,則表示JVM還存在

 public class demo {
     public static void main(String[] args) {
         Thread a = new Thread(() -> {
             System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
             while (true){}
         }, "a");
 ​
         a.start();
         System.out.println(Thread.currentThread().getName());
     }
 }

image-20211212172742883

守護執行緒

ex:垃圾回收 沒有用戶執行緒了,都是守護執行緒,JVM結束

 public class demo {
     public static void main(String[] args) {
         Thread a = new Thread(() -> {
             System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
             while (true){}
         }, "a");
         //設置子執行緒為守護執行緒
         a.setDaemon(true);
         a.start();
         System.out.println(Thread.currentThread().getName());
     }
 }

image-20211212172919606