JUC-JUC是什么?


一、JUC是什么?

java.util.concurrent在並發編程中使用的工具類

進程/線程回顧

1、進程/線程是什么?

進程:進程是一個具有一定獨立功能的程序關於某個數據集合的一次運行活動。它是操作系統動態執行的基本單元,在傳統的操作系統中,進程既是基本的分配單元,也是基本的執行單元。 

線程:通常在一個進程中可以包含若干個線程,當然一個進程中至少有一個線程,不然沒有存在的意義。線程可以利用進程所擁有的資源,在引入線程的操作系統中,通常都是把進程作為分配資源的基本單位,而把線程作為獨立運行和獨立調度的基本單位,由於線程比進程更小,基本上不擁有系統資源,故對它的調度所付出的開銷就會小得多,能更高效的提高系統多個程序間並發執行的程度。

2、進程/線程的例子?

 使用 QQ ,查看進程一定有一個 QQ.exe 的進程,我可以用 qq 和 A 文字聊天,和 B 視頻聊天,給 C 傳文件,給 D 發一段語言, QQ 支持錄入信息的搜索。 

大四的時候寫論文,用 word 寫論文,同時用 QQ 音樂放音樂,同時用 QQ 聊天,多個進程。 

word 如沒有保存,停電關機,再通電后打開 word 可以恢復之前未保存的文檔, word 也會檢查你的拼寫,兩個線程:容災備份,語法檢查

3、線程狀態?

Thread.State 

 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 ;(終結)
} 

 

 

線程的狀態

NEW :新建一個線程

RUNNABLE:准備就緒

BLOCKED:阻塞

WAITING:死等

TIMED_WAITING:過時不候

TERMINATED:終結者

4、wait/sleep區別?

wait/sleep 

功能都是當前線程暫停,有什么區別? 

wait放開手去睡,放開手里的鎖sleep握緊手去睡,醒了手里還有鎖

5、什么是並發?什么是並行?

並發:同一時刻多個線程在訪問同一個資源,多個線程對一個點      例子:小米9今天上午10點,限量搶購 

            春運搶票            電商秒殺... 

並行:多項工作一起執行,之后再匯總      例子:泡方便面,電水壺燒水,一邊撕調料倒入桶中 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM