<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    I want to fly higher
    programming Explorer
    posts - 114,comments - 263,trackbacks - 0
    1.Thread API
        
    1. public final void join() throws InterruptedException {
          ->Waits for this thread to die

         2.public final synchronized void join(long millis) throws InterruptedException {
          ->Waits at most {@code millis} milliseconds for this thread to die. A timeout of {@code 0} means to wait forever.
          {@link Object#wait}

         3.public void interrupt()
          -> Interrupts this thread
          -> If this thread is blocked in an invocation of the {@linkObject#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link Object#wait(long, int) wait(long, int)} methods of the {@link Object} class, or of the {@link #join()}, {@link #join(long)}, {@link #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},methods of this class, then its interrupt status will be cleared and it will receive an {@link InterruptedException}.
          ->If this thread is blocked in an I/O operation upon an {@link java.nio.channels.InterruptibleChannel </code>interruptible channel<code>} then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a {@link java.nio.channels.ClosedByInterruptException}.
          -> If this thread is blocked in a {@link java.nio.channels.Selector}then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's {@link java.nio.channels.Selector#wakeup wakeup} method were invoked.

    2.Object API
         1.public final void wait() throws InterruptedException
          ->Causes the current thread to wait until another thread invokes the {@link java.lang.Object#notify()} method or the{@link java.lang.Object#notifyAll()} method for this object. In other words, this method behaves exactly as if it simply performs the call {@code wait(0)}.
          ->The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the {@code notify} method or the{@code notifyAll} method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

          2.public final native void notify()
          ->Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened.

    3.多線程之間協作
         1.Object.wait()在釋放CPU同時,釋放了對象鎖的控制;

         2.Obj.wait(),與Obj.notify()必須要與synchronized(Obj)一起使用,也就是wait,與notify是針對已經獲取了Obj鎖進行操作,從語法角度來說就是Obj.wait(),Obj.notify必須在synchronized(Obj){...}語句塊內。從功能上來說wait就是說線程在獲取對象鎖后,主動釋放對象鎖,同時本線程休眠。直到有其它線程調用對象的notify()喚醒該線程,才能繼續獲取對象鎖,并繼續執行;

         3.a.調用obj.wait()后,線程A就釋放了obj的鎖,否則線程B無法獲得obj鎖,也就無法在synchronized(obj) {...} 代碼段內喚醒A
           b.當obj.wait()方法返回后,線程A需要再次獲得obj鎖,才能繼續執行
           c.如果A1,A2,A3都在obj.wait(),則B調用obj.notify()只能喚醒A1,A2,A3中的一個,JVM決定
           d.obj.notifyAll()則能全部喚醒A1,A2,A3,但是要繼續執行obj.wait()的下一條語句,必須獲得obj鎖,因此,A1,A2,A3只有一個有機會獲得鎖繼續執行如A1,其余的需要等待A1釋放obj鎖之后才能繼續執行
           e.當B調用obj.notify/notifyAll的時候,B正持有obj鎖,因此,A1,A2,A3雖被喚醒,但是仍無法獲得obj鎖。直到B退出synchronized塊,釋放obj鎖后A1,A2,A3中的一個才有機會獲得鎖繼續執行

         4.Java的synchronized類似于操作系統概念中的互斥內存塊->JAVA中對象內存鎖->線程獲取該內存鎖后,其它線程無法訪問該內存->(static(所有對象共享的內存)和對象的區別)

          5.當一個線程請求其它的線程已經占有的鎖時,請求線程將被阻塞。然而內部鎖是可重進入的,因此線程在試圖獲得它自己占用的鎖時請求會成功->重進入的實現是通過為每一個鎖關聯一個請求計數器和一個占有他的線程->當計數為0時,認為鎖是未被占用的->線程請求一個未被占有的鎖時候,JVM將記錄鎖的占有者,并且將請求計數設置為1->如果同一個線程再次請求這個鎖,計數將遞增->每次占用線程退出語句塊時,計數器值將遞減,直到計數器達到0時候,鎖被釋放.

    4.方法理解
         1.join->A線程中調用->B線程的join方法B.join()->B線程繼續運行,A線程停止(進入阻塞狀態)->等B運行完畢A再繼續運行,中途B線程不受影響->A線程停止的位置就在調用join方法處->后續代碼不執行->要等B完了以后再執行->
          ->join,合并->調用b.join->b線程合并到a線程->a線程等待b線程運行完再運行.(landon理解:之所以稱為join,合并->將t.join的線程t合并到調用的當前線程->因為合并了t->所以要先執行t(相當于在當前線程的執行過程中插播了一段t[合并了t]))->

         2.sleep->
             1.public static native void sleep(long millis) throws InterruptedException->靜態方法->
             2.當前線程進入暫停狀態->指定的sleep時間內都不會獲得執行->
             3.可以使優先級低的線程 | 同優先級的線程 | 高優先級的線程 獲得執行的機會(是否取決于當前線程的優先級)

         3.yield->
          1.public static native void yield()->靜態方法->
          2.線程讓步->如果具有相同優先級的其他線程處于就緒狀態->yield()方法將把當前運行的線程放到可運行池中并使另一個線程運行->如果沒有相同優先級的可運行進程->yield什么都不做->
          3.yield->使當前線程重新回到可執行狀態->執行yield()的線程有可能在進入到可運行狀態后->馬上又被運行->

         4.interrupt->
          1.interrupt->不會中斷一個正在運行的線程->這一方法實際上完成的是->在線程受到阻塞時拋出一個中斷信號->這樣線程就得以退出阻塞的狀態->如果線程被Object.wait, Thread.join和Thread.sleep三種方法之一阻塞->它將接收到一個中斷異常(InterruptedException)->從而提早地終結被阻塞狀態->

          2.線程被上述幾種方法阻塞->正確的停止線程方式是設置共享變量->并調用interrupt()(注意變量應該先設置)->如果線程沒有被阻塞,這時調用interrupt()將不起作用->否則線程就將得到異常(該線程必須事先預備好處理此狀況->try/catch(InterruptedException)),接著逃離阻塞狀態->在任何一種情況中,最后線程都將檢查共享變量然后再停止->
       (landon理解:之前我停止線程的方法是向主線程消息隊列投遞一個停止消息->即不通過多線程的方式->而是全部通過單線程消息隊列投遞消息->然后在單線程消息處理器中將stopFlag置為true)

          3.public final void join() throws InterruptedException
             public static native void sleep(long millis) throws InterruptedException
             public final void join() throws InterruptedException
             LinkedBlockingQueue#public E take() throws InterruptedException->
             public final void wait() throws InterruptedException
            ->目前上述方法在其線程上調用interrupt方法->都會拋出InterruptedException

          4.每個線程都有一個與之相關聯的 boolean 屬性->用于表示線程的中斷狀態(interrupted status)->中斷狀態初始時為 false->當另一個線程通過調用 Thread.interrupt() 中斷一個線程時,會出現以下兩種情況之一->如果那個線程在執行一個低級可中斷阻塞方法,例如 Thread.sleep()、 Thread.join() 或 Object.wait(),那么它將取消阻塞并拋出 InterruptedException->否則 interrupt() 只是設置線程的中斷狀態-> 在被中斷線程中運行的代碼以后可以輪詢中斷狀態,看看它是否被請求停止正在做的事情->中斷狀態可以通過 Thread.isInterrupted() 來讀取,并且可以通過一個名為 Thread.interrupted() 的操作讀取和清除->
           public static boolean interrupted() { return currentThread().isInterrupted(true);}

          5.中斷是一種協作機制->當一個線程中斷另一個線程時,被中斷的線程不一定要立即停止正在做的事情->相反,中斷是禮貌地請求另一個線程在它愿意并且方便的時候停止它正在做的事情。有些方法,例如 Thread.sleep(),很認真地對待這樣的請求,但每個方法不是一定要對中斷作出響應->

          6.并非所有的阻塞方法都拋出 InterruptedException。輸入和輸出流類會阻塞等待 I/O 完成,但是它們不拋出 InterruptedException,而且在被中斷的情況下也不會提前返回(輪詢中斷狀態?)->對于套接字 I/O,如果一個線程關閉套接字,則那個套接字上的阻塞 I/O 操作將提前結束,并拋出一個 SocketException。java.nio 中的非阻塞 I/O 類也不支持可中斷 I/O,但是同樣可以通過關閉通道或者請求 Selector 上的喚醒來取消阻塞操作->嘗試獲取一個內部鎖的操作(進入一個 synchronized 塊)是不能被中斷的,但是 ReentrantLock 支持可中斷的獲取模式->

    5.推薦的一篇文章
        http://www.ibm.com/developerworks/cn/java/j-jtp05236.html

    6.線程狀態

         1.New(初始狀態)->t.start->Runnable(可運行狀態/就緒狀態)->os時間片輪轉,t獲得cpu時間片->Running(運行狀態)->run結束->Dead(死亡狀態)
         2.Running(運行狀態)->t2.join/sleep/io->Blocked(阻塞狀態)->sleep結束/t2終止/io完成->Runnable(可運行狀態/就緒狀態)
         3.Running(運行狀態)->時間片用完/t.yield->Runnable(可運行狀態/就緒狀態)
         4.Running(運行狀態)->t線程調用o.wait->等待隊列->其他線程調用o.notify/o.nonifyAll->鎖池隊列->拿到對象的鎖->Runnable(可運行狀態/就緒狀態)
         5.Running(運行狀態)->執行synchronized塊->鎖池隊列->拿到對象的鎖->Runnable(可運行狀態/就緒狀態)

        {@link 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;
        }
        

    7.守護線程
        public final void setDaemon(boolean on)->
         1.->Marks this thread as either a  daemon thread(守護線程) or a user thread(用戶線程). The Java Virtual Machine exits when the only threads running are all daemon threads.
         2.守護線程為用戶線程服務->JVM的垃圾回收、內存管理等線程都是守護線程->還有就是在做數據庫應用時候,使用的數據庫連接池,連接池本身也包含著很多后臺線程,監控連接個數、超時時間、狀態等->User Thread已經全部退出運行了->只剩下Daemon Thread存在了->虛擬機退出->因為沒有被守護的用戶線程了->守護線程存在就無意義了->
         3.thread.setDaemon(true)必須在thread.start()之前設置->否則會跑出一個IllegalThreadStateException異常->你不能把正在運行的常規線程設置為守護線程
         4.在Daemon線程中產生的新線程也是Daemon的->
         5.通常server shutdown的線程設置為守護線程->該首付線程負責shutdown所有的用戶線程->在用戶線程shutdown ok->jvm退出->shutdown的守護線程自然退出->不要把一些應用的業務放在守護線程做->否則當用戶線程結束的時候,jvm就退出了->而此時可能守護線程的task還未執行完畢->

    8.
    關于阻塞IO
        線程阻塞->放棄CPU,暫停運行->只有等待導致阻塞的原因消除->或者被其他線程中斷->則退出阻塞狀態->并拋出InterruptedException.

      線程阻塞原因:
       1.Thread.sleep(long n)->線程放棄CPU->睡眠n毫秒,然后恢復運行->

       2.線程執行一段同步代碼->由于無法獲得同步鎖->阻塞->直到獲得同步鎖->恢復運行->

       3.線程執行了一個對象的wait->進入阻塞->等待其他線程執行了該對象的notify()或notifyAll喚醒

       4.線程執行io操作或遠程通信->會因為等待相關的資源而進入阻塞狀態->如System.in.read()->
        遠程通信,Client可能進入阻塞狀態->
         1.Socket#connect->阻塞->直到連接成功
         2.Socket#read->沒有足夠的數據->阻塞->讀到了足夠的數據/輸入流末尾/異常->才會返回或者異常中斷-> read()->輸入流中只需要一個字節就足夠->read(byte[] buff)->只要輸入流中的字節數目與buff數組的長度相同即足夠->readLine->只要輸入流中有一行字符串就足夠(BufferedReader)
         3.Socket#write->阻塞->直到輸出了所有的數據或者出現異常->才返回或者異常中斷
         4.Socket#setSoLinger->設置關閉Socket延遲時間->Socket#close->進入阻塞->直到底層Socket發送完所有剩余數據或者到了SoLinger的超時時間->返回

          Server也可能進入阻塞狀態->
         1.ServerSocket#accept->直到接收到了Client連接才返回
         2.#read->如果輸入流沒有足夠的數據->阻塞
         3.#write->阻塞->直到輸出了所有數據或出現異常->

       ->無論是Client還是Server->通過Socket輸入輸出流讀寫數據->都可能進入阻塞狀態->阻塞IO->如果執行輸入輸出操作時->不發生阻塞->非阻塞IO   
    posted on 2013-08-06 12:40 landon 閱讀(1856) 評論(1)  編輯  收藏 所屬分類: Program

    FeedBack:
    # re: Java線程小結Ⅰ
    2013-08-06 16:22 | 快開工具
    看了就頂一下!  回復  更多評論
      
    主站蜘蛛池模板: www.91亚洲| 亚洲av乱码一区二区三区| 91久久青青草原线免费| 久久亚洲最大成人网4438| 免费v片在线观看品善网| a级毛片毛片免费观看永久| 亚洲午夜电影在线观看高清 | 亚洲国产女人aaa毛片在线| 国产成人yy免费视频| 野花视频在线官网免费1| 亚洲欧洲第一a在线观看| 天天摸夜夜摸成人免费视频| 二个人看的www免费视频| ass亚洲**毛茸茸pics| 亚洲精品无码久久毛片| 久久久久久久91精品免费观看| 免费无遮挡无遮羞在线看| 91亚洲自偷在线观看国产馆| 亚洲精品国精品久久99热| 亚洲精品免费网站| 999zyz**站免费毛片| 亚洲精品无码永久在线观看男男| 亚洲va国产va天堂va久久| 国产精品久免费的黄网站| 在线观看免费中文视频| 永久免费观看黄网站| 国产亚洲精品影视在线| 亚洲avav天堂av在线不卡| 亚洲精品亚洲人成在线观看下载| 免费可以在线看A∨网站| 嫩草在线视频www免费观看 | 国产成人免费爽爽爽视频| 男女作爱在线播放免费网站| 国产亚洲综合一区二区三区| 亚洲日韩乱码中文无码蜜桃臀| 亚洲中文字幕无码久久2017| 免费少妇a级毛片人成网| 成人看的午夜免费毛片| 久久久久久精品免费免费自慰| 国产一级高青免费| 一级午夜免费视频|