1、前言java
主要分紅兩部提及:Thread源碼解讀和常見面試題解答,廢話很少說開始;面試
2、源碼解讀app
首先看下構造函數,構造函數都是經過調用init方法對屬性進行初始化,主要是對線程組、線程名字、棧大小等信息進行初始化;init內部經過調用currentThread本地方法,獲取當前的線程,這個本地方法封裝在JVM中,有興趣的能夠看下這個這個連接查找下JVM實現https://hg.openjdk.java.net/jdk8u,接下來對ThreadGroup的判斷,若是沒有傳入線程組的話, 第一是使用SecurityManager中的ThreadGroup, 若是從SecurityManager 中獲取不到ThreadGroup(), 那麼就從當前線程中獲取線程組,最後作了檢驗和些參數的賦值,總體上相對比較簡單;less
private void init(ThreadGroup g, Runnable target, String name, long stackSize) { init(g, target, name, stackSize, null); } private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc) { if (name == null) { throw new NullPointerException("name cannot be null"); } this.name = name.toCharArray(); Thread parent = currentThread(); SecurityManager security = System.getSecurityManager(); if (g == null) { /* Determine if it's an applet or not */ /* If there is a security manager, ask the security manager what to do. */ if (security != null) { g = security.getThreadGroup(); } /* If the security doesn't have a strong opinion of the matter use the parent thread group. */ if (g == null) { g = parent.getThreadGroup(); } } /* checkAccess regardless of whether or not threadgroup is explicitly passed in. */ g.checkAccess(); /* * Do we have the required permissions? */ if (security != null) { if (isCCLOverridden(getClass())) { security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION); } } g.addUnstarted(); this.group = g; this.daemon = parent.isDaemon(); this.priority = parent.getPriority(); if (security == null || isCCLOverridden(parent.getClass())) this.contextClassLoader = parent.getContextClassLoader(); else this.contextClassLoader = parent.contextClassLoader; this.inheritedAccessControlContext = acc != null ? acc : AccessController.getContext(); this.target = target; setPriority(priority); if (parent.inheritableThreadLocals != null) this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); /* Stash the specified stack size in case the VM cares */ this.stackSize = stackSize; /* Set thread ID */ tid = nextThreadID(); } public Thread() { init(null, null, "Thread-" + nextThreadNum(), 0); } public Thread(Runnable target) { init(null, target, "Thread-" + nextThreadNum(), 0); } Thread(Runnable target, AccessControlContext acc) { init(null, target, "Thread-" + nextThreadNum(), 0, acc); } // 線程名 public Thread(String name) { init(null, null, name, 0); } //線程組和線程名 public Thread(ThreadGroup group, String name) { init(group, null, name, 0); } //線程任務,線程名 public Thread(Runnable target, String name){ init(null, target, name, 0); } // 線程組, 線程任務, 線程名 ,棧大小 public Thread(ThreadGroup group, Runnable target, String name, long stackSize) { init(group, target, name, stackSize); }
接下來看下主要的屬性:ide
// 類加載的時候,調用本地的註冊本地方靜態方法, 這個方法是本地方法 private static native void registerNatives(); static { registerNatives(); } private volatile char name[]; private int priority; private Thread threadQ; private long eetop; /* Whether or not to single_step this thread. */ private boolean single_step; /* Whether or not the thread is a daemon thread. */ // 設設置這個線程是不是守護線程 private boolean daemon = false; /* JVM state */ private boolean stillborn = false; /* What will be run. */ // 要執行的run方法的對象 private Runnable target; /* The group of this thread */ // 這個線程的線程組 private ThreadGroup group; /* The context ClassLoader for this thread */ // 這個線程的上下文類加載器 private ClassLoader contextClassLoader; /* The inherited AccessControlContext of this thread */ private AccessControlContext inheritedAccessControlContext; /* For autonumbering anonymous threads. */ private static int threadInitNumber; private static synchronized int nextThreadNum() { return threadInitNumber++; } /* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null; /* * InheritableThreadLocal values pertaining to this thread. This map is * maintained by the InheritableThreadLocal class. */ ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; /* * The requested stack size for this thread, or 0 if the creator did * not specify a stack size. It is up to the VM to do whatever it * likes with this number; some VMs will ignore it. */ // 給這個線程設置的棧的大小,默認爲0 private long stackSize; /* * JVM-private state that persists after native thread termination. */ private long nativeParkEventPointer; /* * Thread ID */ //線程id private long tid; /* For generating thread ID */ private static long threadSeqNumber; /* Java thread status for tools, * initialized to indicate thread 'not yet started' */ private volatile int threadStatus = 0; private static synchronized long nextThreadID() { return ++threadSeqNumber; } /** * The argument supplied to the current call to * java.util.concurrent.locks.LockSupport.park. * Set by (private) java.util.concurrent.locks.LockSupport.setBlocker * Accessed using java.util.concurrent.locks.LockSupport.getBlocker */ volatile Object parkBlocker; /* The object in which this thread is blocked in an interruptible I/O * operation, if any. The blocker's interrupt method should be invoked * after setting this thread's interrupt status. */ private volatile Interruptible blocker; private final Object blockerLock = new Object(); /* Set the blocker field; invoked via sun.misc.SharedSecrets from java.nio code */ void blockedOn(Interruptible b) { synchronized (blockerLock) { blocker = b; } } /** * The minimum priority that a thread can have. */ // 線程執行的最低優先級 爲1 public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ // 線程默認的執行優先級爲 5 public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ // 線程執行的最高的優先級爲 10 public final static int MAX_PRIORITY = 10;
最後介紹下方法的做用和線程狀態,源碼都比較簡單,沒必進行過多的介紹,都是經過調用JVM的本地方法實現;函數
線程狀態:ui
三、常見面試題this
1.線程與進程的區別?spa
進程是資源分配最小的單位,線程是CPU調度最小的單位;.net
線程屬於進程,共享進程分配的資源;
進程屬於搶佔式調度,資源不相互共享;
2.start和run的區別?
run是Thread的一個普通的方法;
start方法會建立一個新的子線程並啓動;
3.sleep與wait的區別?
sleep是Thread方法,wait是Object的方法;
wait方法只能在synchroized方法或者塊中使用;
Thread.sleep只會讓出CPU,不會改變鎖的行爲;
Object.wait不只會讓出CPU,同時還會釋放佔有同步資源的鎖;
4.線程狀態的轉化?
圖中將WAITING 和TIMED_WAITING 兩個狀態合併爲WAITING ,沒有分開,你們不要搞錯;
5.如何處理線程的返回值?
主線程等待法,使用while等待主線程返回值;
join阻塞當前線程以等待子線程;
經過FuTureTask獲取子線程的返回值;
public class MyCallable implements Callable<String> { @Override public String call() throws Exception { String value="test"; System.out.println("start"); Thread.sleep(5000); System.out.println("end"); return value; } } public class FutureTaskDemo { public static void main(String[] main) throws ExecutionException, InterruptedException { FutureTask<String> futureTask=new FutureTask<String>(new MyCallable()); new Thread(futureTask).start(); if (!futureTask.isDone()){ System.out.println("waiting"); } System.out.println("return"+futureTask.get()); } }
經過線程池獲取返回值;
public class ThreadPoolDemo { public static void main(String[] args){ ExecutorService executorService= Executors.newCachedThreadPool(); Future<String> futureTask=executorService.submit(new MyCallable()); if (!futureTask.isDone()){ System.out.println("wait"); } try { System.out.println(futureTask.get()); }catch (InterruptedException ex){ ex.printStackTrace(); }catch (ExecutionException ex){ ex.printStackTrace(); }finally { executorService.shutdown(); } } }
6.Thread和Runnable?
Thread是類,Runnable是接口,Thread是Runnable實現;
類的繼承單一原則,Runnable是更高層次的抽象;
4、結束
歡迎你們加羣438836709!歡迎你們關注我!