1. 程式人生 > 其它 >Java多執行緒面試題小結

Java多執行緒面試題小結

1.執行緒狀態

下面是JDK中定義的執行緒狀態

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; }

  把其中的Runnable狀態時是否獲取到cpu時間片,可為Runnable和Running討論,就有下面一張圖

2.建立執行緒

  1.繼承Thread類 2.實現Runnable介面 3.實現Callable介面(有返回值,可丟擲異常) 4執行緒池executors

3.有關執行緒的幾個方法

  1.sleep  睡眠,不釋放鎖。

  2.wait   進入等待佇列,釋放鎖。

  3.notify  喚醒等待佇列的某個執行緒。和wait一樣必須在synchronized程式碼塊中使用。

  4.yield   讓步,由立即進入就緒狀態。

  5.await和signal condition類的方法,對應wait和notify(Object類),一個condition相當於一個鎖池。

4.手寫死鎖

public class DeadLock {
    public static void main(String[] args) {
        Thread thread = new DeadLockThread("Thread1");
        Thread thread2 = new DeadLockThread("Thread2");
        thread.start();
        thread2.start();
    }
}
class DeadLockThread extends Thread{
    private static final Object A = new Object();
    private static final Object B = new Object();
    public DeadLockThread(String name){
        setName(name);
    }
    @Override
    public void run() {
        if ("Thread1".equals(getName())){
            synchronized (A){
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread1StartAndGetLockA");
                synchronized (B){
                    System.out.println("Thread1StartAndGetLockB");
                }
            }
        }
        if ("Thread2".equals(getName())){
            synchronized (B){
                System.out.println("Thread2StartAndGetLockB");
                synchronized (A){
                    System.out.println("Thread2StartAndGetLockA");
                }
            }
        }
    }
}

5.執行緒池

  ThreadPoolExecutor幾個引數

  核心執行緒數,最大執行緒數,存活時間,單位,工作佇列,執行緒工廠,拒絕策略。

  執行順序:用銀行櫃檯理解,常駐櫃檯(核心執行緒數),常駐櫃檯加臨時櫃檯(最大執行緒數),等待區(工作佇列),先排常駐櫃檯和等待區,全都滿了開臨時櫃檯視窗。

6.鎖升級

  CAS使用者態,自旋佔用CPU,重量級鎖,核心態,不佔CPU資源。(待消化)