As always, we look directly at the JDK code cut-in:
The first is the simplest runnable interface:
Public Interface Runnable { publicabstractvoid run ();}
We can see runnable is actually very simple, is the interface, there is only one method (in fact, public abstract is not necessary, but not the old version of the JDK need to add it is unclear).
The main object we need to analyze is the thread class:
PublicclassThreadImplementsthe Runnable {---thread class implements the Runnable interface, which is the need to overwrite the Run method Private Static native voidregisternatives (); Static{registernatives ();---register the local method in the class--must be called before all local methods are initialized;}Private Charname[];---The name of the threadPrivate intpriority;---thread priority, thread priority in the thread class has a constant definition, a number between 1-10, if the occurrence of a defined priority exceeds this interval will be reported illegalargumentexception ()
Public final static int min_priority = minimum priority of 1;---thread
Public final static int norm_priority = 5;---Thread's default priority
Public final static int max_priority = maximum priority of 10;---thread
Private Thread threadq;------here to delete some of the defined but useless code (PS: It seems that the JDK code also has so many so-called "reserved" fields, and later estimates are gone).
Private Boolean daemon = false;--is a background thread, that is, a daemon thread, if there is a user thread (that is, a non-background thread) The background thread will persist until there is no user thread to automatically terminate.
It is important to note that if you want to Setdaemon (true), you must do so before Thread.Start (),
cannot be set for a thread after start. Also, because the background thread ends automatically after the user thread is gone, try not to manipulate some system resources.
Private Runnable target; ---to invoke the Runnable object of the Run method, which is actually the current thread
Private Threadgroup group;---objects that are grouped into threads, which can be specified when the thread is initialized;
Note A thread group can contain a thread group, that is, a tree-like thread structure that can have priority settings, daemons, and so on for the entire group of threads.
Private ClassLoader contextclassloader;---class loader that can be customized
private static int threadinitnumber;---The self-increment thread number of the anonymous thread
private static synchronized int nextthreadnum () {
return threadinitnumber++;
}
Threadlocal.threadlocalmap Threadlocals = Null;---threallocalmap This is not explained for the moment, follow-up will have special instructions, thread private theadlocal<t> analysis
Private long tid;---The ID of the current thread
private static long threadseqnumber;--thread Auto ID
private static synchronized long Nextthreadid () {--line Cheng ID
return ++threadseqnumber;
}
private volatile int threadstatus = 0;--is defined in thread state vm.class:
public static state tothreadstate (int var0) {
Return (VAR0 & 4)! = 0? State.runnable: ((VAR0 & 1024)! = 0? State.blocked: ((VAR0 & 16)! = 0? State.waiting: ((VAR0 & 32)! = 0? State.timed_waiting: ((VAR0 & 2)! = 0? State.terminated: ((var0 & 1) = = 0? State.NEW:State.RUNNABLE)));
}
There is currently a dedicated state enumeration to represent the status of the thread: new\runnable\blocked\waiting\timed_waiting\terminated\, which will be followed by a detailed parsing of the meaning of the state to appear in the scene.
Volatile Object parkblocker;---is used to record who is blocking the current thread
Private volatile interruptible blocker;
Private Final Object Blockerlock = new Object ();
public static native thread CurrentThread ();---Returns a reference to the current thread of execution
public static native void yield ()----indicates that the current thread is willing to hand over CPU time for use by other threads, but whether or not it is actually delivered is not necessarily true. This method is actually rarely used in actual scenarios, but many interviews will ask.
public static native void sleep (long Millis) throws interruptedexception;--sleep waits, does not release the sync lock (and wait is the difference between a frequently asked question, wait release lock, And wait is the method of object)
---Next is a bunch of various overloaded construction parameters and Init methods, not to repeat.
Public synchronized void Start ()-Start, determine whether the status is 0, otherwise error
@Override
public void Run () {----rewrite run
if (target! = null) {
Target.run ();
}
}
private void Exit () {-----allows the thread to be recycled before it really ends
if (group! = null) {
Group.threadterminated (this);
group = NULL;
}
Threadlocals = null;
Inheritablethreadlocals = null;
Inheritedaccesscontrolcontext = null;
blocker = null;
Uncaughtexceptionhandler = null;
}
---Several deprecated stop methods, do not repeat
public void interrupt () {
if (This! = Thread.CurrentThread ())
CheckAccess ();
Synchronized (Blockerlock) {
Interruptible b = blocker;
if (b! = null) {
Interrupt0 (); Just to set the interrupt flag
B.interrupt (this);
Return
}
}
Interrupt0 ();
}
---Next there are the get and set methods of the attributes described above, and the suspend and resume methods currently being deprecated;
Public stacktraceelement[] Getstacktrace ()--Get the stack information of the thread
Some of the content is not posted, feel that the general interview will not be asked, at the same time, under normal circumstances do not care about the content.
Thread-related code analysis-common face questions (I., THEAD Class)