1, runnable interface Source:
1 Public Interface 2publicabstractvoid 3}
2. The inheritance relationship between thread class and runnable interface
1 Public class Implements 23 }
The runnable interface has only one run () method, and the thread class implements the Runnable interface, so the thread class also implements the Runnable interface.
3. Constructor function
1 Public 2 init (nullnull, "thread-" + nextthreadnum (), 03 }
1 Public 2 init (null, Target, "thread-" + nextthreadnum (), 03 }
1 Public 2 init (group, Target, "thread-" + nextthreadnum (), 03 }
1 Public 2 init (nullnull, name, 03 } There are other construction methods, omitted here ...
The third parameter here is to set the name of the thread, as can be seen from the following code: "thread-" plus the number of threads created (the first few).
Continue to view the Init method:
1 /** 2 * Initializes a Thread.3 * 4 * @paramg The Thread group5 * @paramtarget the object whose run () method gets called6 * @paramname The name of the new Thread7 * @paramstackSize the desired stack size for the new thread, or8 * Zero to indicate the This parameter are to be ignored.9 */ //threadgroup: A thread group represents a collection of threads. In addition, thread groups can contain other thread groups. Thread groups form a tree in which each thread group has a parent thread group, in addition to the initial thread group. Ten Private voidInit (threadgroup g, Runnable Target, String name, One LongstackSize) { AThread parent =CurrentThread (); -SecurityManager Security =System.getsecuritymanager (); - if(g = =NULL) { the /*determine if it ' s an applet or not*/ - - /*If There is a security manager, ask the security manager - What does .*/ + if(Security! =NULL) { -g =Security.getthreadgroup (); + } A at /*If The security doesn ' t have a strong opinion of the matter - Use the parent thread group.*/ - if(g = =NULL) { -g =Parent.getthreadgroup (); - } - } in - /*checkAccess regardless of whether or not Threadgroup are to explicitly passed in.*/ + g.checkaccess (); - the /* * * Do we have the required permissions? $ */ Panax Notoginseng if(Security! =NULL) { - if(Isccloverridden (GetClass ())) { the security.checkpermission (subclass_implementation_permission); + } A } the + - g.addunstarted (); $ $ This. Group =G;
// each thread has a priority, and the execution of the high-priority thread takes precedence over the low-priority thread. Each thread can or cannot be marked as a daemon. When code running in a thread creates a new thread object, the initial priority of the new thread is set to the priority of the thread being created, and the new thread is the daemon only if the creation thread is the daemon thread.
- This. Daemon =Parent.isdaemon (); - This. Priority =parent.getpriority (); the This. Name =Name.tochararray (); - if(Security = =NULL||Isccloverridden (Parent.getclass ()))Wuyi This. Contextclassloader =Parent.getcontextclassloader (); the Else - This. Contextclassloader =Parent.contextclassloader; Wu This. Inheritedaccesscontrolcontext =Accesscontroller.getcontext (); - This. target =Target; About setpriority (priority); $ if(Parent.inheritablethreadlocals! =NULL) - This. inheritablethreadlocals = - Threadlocal.createinheritedmap (parent.inheritablethreadlocals); - /*Stash The specified stack size in case the VM cares*/ A This. stackSize =stackSize; + the /*Set thread ID*/ -Tid =Nextthreadid (); $}
Initializes the name if it is a daemon thread, priority, and initialization.
4, the thread of the Start method implementation:
1 Public synchronized voidstart () {2 /** 3 * This method isn't invoked for the main method thread or "system"4 * Group Threads Created/set up by the VM. Any new functionality added5 * To the The also is added to the VM.6 * 7 * A Zero status value corresponds to state "NEW".8 */ 9 if(Threadstatus! = 0) Ten Throw Newillegalthreadstateexception (); OneGroup.add ( This); A start0 (); - if(Stopbeforestart) { - stop0 (throwablefromstop); the } -}
Here the main is the Start0 method; see its implementation:
1 Private native void start0 ();
Here, a local call is used to initialize the system resources required by the thread through C code. It can be seen that the implementation of the thread bottom is done by C code.
4. Implementation of thread's Run method
1 Public void 2ifnull 3 45}
The target here is actually to save a reference to the implementation of an Runnable interface:
1 Private Runnable Target;
So when you create a thread class with inherited thread, you need to override the Run method because the default run method does nothing.
When we implement the thread class using the Runnable interface, in order to start the thread, we need to initialize the thread class instance to a single thread, and actually execute the following constructor:
1 Public 2 init (null, Target, "thread-" + nextthreadnum (), 03 }
That is, the reference to the thread class is saved to target. This way, when the thread's Run method is called, Target is not empty, but it continues to invoke the target's run method, so we need to implement the Runnable run method. This invokes the Run method in the Runnable implementation class through the thread's Run method.
This is why the thread class that the Runnable interface implements needs to be started like this.
Java Thread Source Code Analysis