《java多线程编程实战指南 核心篇》读书笔记一
? ? 1. run方法是線程的任務處理邏輯的入口方法,它由java虛擬機在運行相應線程時直接調用,而不是由應用代碼進行調用。
? ? 2. 啟動一個線程的實質是請求java虛擬機運行相應的線程,而這個線程具體何時能夠運行是由線程調度器(Scheduler)(線程調度器是操作系統的一部分)決定的。
? ?3. 在java平臺中,一個線程是一個對象,對象的創建離不開內存空間的分配. 創建一個線程與創建其他類型的java對象所不同的是,java虛擬機會為每個線程分配調用棧(call stack)所需的內存空間。調用棧用于跟蹤java代碼(方法)間的調用關系以及java代碼對本地代碼(Native Code, 通常是C代碼)的調用。
? ?4. java平臺中的任意一段代碼(比如一個方法)總是由確定的線程負責執行的,這個線程就相應地被稱為這段代碼的執行線程。
? ?5. 多個線程實例可以共享同一個Runnable實例,這時候就會出現線程安全問題。
? ?6. 線程的層次關系: ? java平臺中,線程不是孤立存在的,線程與線程之間總是存在一些聯系。假如A線程所執行的代碼創建了線程B,那么習慣上稱線程B為線程A的子線程,相應的線程A就被稱為線程B的父線程。
?Thread.java源碼中的枚舉類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;}7.? 多線程編程的實質就是將任務的處理方式由串行改為并發,即實現并發化,以發揮并發的優勢。
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的《java多线程编程实战指南 核心篇》读书笔记一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单调栈-窗口
- 下一篇: 《java多线程编程实战指南 核心篇》读