java面試題

來源:互聯網
上載者:User

標籤:positive   喚醒   join()   線程   runnable   ecif   連結   pre   sso   

  最近看到一份面試題,覺得寫的還不錯,雖然作者只給出了題目沒有答案,但是它既是一份面試題同樣作者其實很好的總結了java程式員應該具備的技術體系,希望能夠對初入java和迷茫不知學什麼的同學以指引

原文連結

由於某些原因正打算換工作,所以試著整理解答,答案只是我的想法,如有不對之處歡迎大神批評指正,

由於題目太多,我會持續添加

  1. Java線程的狀態

 1  public enum State { 2         /** 3          * Thread state for a thread which has not yet started. 4          */ 5         NEW, 6  7         /** 8          * Thread state for a runnable thread.  A thread in the runnable 9          * state is executing in the Java virtual machine but it may10          * be waiting for other resources from the operating system11          * such as processor.12          */13         RUNNABLE,14 15         /**16          * Thread state for a thread blocked waiting for a monitor lock.17          * A thread in the blocked state is waiting for a monitor lock18          * to enter a synchronized block/method or19          * reenter a synchronized block/method after calling20          * {@link Object#wait() Object.wait}.21          */22         BLOCKED,23 24         /**25          * Thread state for a waiting thread.26          * A thread is in the waiting state due to calling one of the27          * following methods:28          * <ul>29          *   <li>{@link Object#wait() Object.wait} with no timeout</li>30          *   <li>{@link #join() Thread.join} with no timeout</li>31          *   <li>{@link LockSupport#park() LockSupport.park}</li>32          * </ul>33          *34          * <p>A thread in the waiting state is waiting for another thread to35          * perform a particular action.36          *37          * For example, a thread that has called <tt>Object.wait()</tt>38          * on an object is waiting for another thread to call39          * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on40          * that object. A thread that has called <tt>Thread.join()</tt>41          * is waiting for a specified thread to terminate.42          */43         WAITING,44 45         /**46          * Thread state for a waiting thread with a specified waiting time.47          * A thread is in the timed waiting state due to calling one of48          * the following methods with a specified positive waiting time:49          * <ul>50          *   <li>{@link #sleep Thread.sleep}</li>51          *   <li>{@link Object#wait(long) Object.wait} with timeout</li>52          *   <li>{@link #join(long) Thread.join} with timeout</li>53          *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>54          *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>55          * </ul>56          */57         TIMED_WAITING,58 59         /**60          * Thread state for a terminated thread.61          * The thread has completed execution.62          */63         TERMINATED;64     }

  1)New (建立)

  當用new操作符建立一個新線程時,如 new Thread (runnable);此時該線程還沒有開始

  2)Runnable (可運行狀態)

  當線程有資格運行,但發送器還沒有把它選定為運行線程時線程所處的狀態。當start()方法調用時,線程首先進入可運行狀態。線上程運行之後或者從阻塞、等待或睡眠狀態回來後,也返回到可運行狀態。

  3) Blocked (被阻塞)

  當線程調用sleep()或者join()方法就會進入Blocked狀態,但是要注意的是阻塞的線程是不釋放當前所佔有的系統資源,當sleep()結束或者join()等待其他線程來到,當前線程則進入Runnable狀態等待JVM分配資源。

  4)Waiting(等待)

  如果當前線程調用wait()方法,則當前線程進入Time waiting但是這個時候當前線程會釋放所佔有的JVM資源,進入這個狀態過後是不能自動喚醒的,必須調用notify()或者notifyAll()方法,線程進入Waiting。

  5)Timed Waiting (計時等待)

  當線程進入Runnable狀態,但是還沒有開始啟動並執行時候,此時發現需要的資源處於同步狀態synchronized,這個時候線程將會進入Time waiting,JVM會使用隊列對這些線程進行控制,既先進行Time waiting的線程會先得到JVM資源進行執行進入Waiting

  6)Terminated (被終止)

  線程如果執行run() main()方法結束後,完成邏輯,線程就進入Terminated

java面試題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.