Concurrent programming study notes (seven, Thread source code analysis)

table of Contents:

  • Common attributes
  • Constructor
  • start()
  • run()

Common attributes:

1    / * 
2       * thread name
 . 3       * / 
. 4      Private  volatile String name;
 . 5  
. 6      / ** 
. 7       * thread priority
 . 8       * / 
. 9      Private  int priority;
 10  
. 11      / ** 
12 is       * is a daemon thread, true- be daemon thread
 13 is       * / 
14      Private  Boolean daemon = to false ;
 15  
16      / ** 
. 17       * may be executed by the Runnable
 18 is       * / 
. 19      Private the Runnable target;
 20 is  
21 is      / **
22       * belongs to a thread group
 23 is       * / 
24      Private of ThreadGroup Group;
 25  
26 is      / ** 
27       * current thread belongs to the ThreadLocal
 28       * / 
29      ThreadLocal.ThreadLocalMap ThreadLocals = null ;
 30  
31 is      / ** 
32       * is provided by the child thread parent thread inherited value
 33 is       * / 
34 is      ThreadLocal.ThreadLocalMap inheritableThreadLocals = null ;
 35  
36      / ** 
37 [       * current thread's stack size, if not specified the default is 0; the value depends on how the JVM
 38 is       * / 
39      Private  Long the stackSize;
 40 
41      / ** 
42       * thread ID
 43 is       * / 
44 is      Private  Long TID;
 45  
46 is      / ** 
47       * thread Serial No.
 48       * / 
49      Private  static  Long threadSeqNumber;
 50  
51 is      / ** 
52 is       * thread state, the default is not started
 53 is       * / 
54 is      Private  volatile  int threadStatus = 0 ;
 55  
56 is      / ** 
57 is       * obtained sequence number of the next thread
 58       * / 
59      Private  static  the synchronized  LongnextThreadID () {
 60          return ++ threadSeqNumber;
 61 is      }
 62 is  
63 is      / ** 
64       * provides variable java.util.concurrent.locks.LockSupport.park
 65       * / 
66      volatile Object parkBlocker;
 67  
68      / ** 
69       * obstruction lock, blocking the main processing conditions
 70       * / 
71 is      Private  volatile the Interruptible blocker;
 72  
73 is      / ** 
74       * lock block
 75       * / 
76      Private  Final Object blockerLock = new new Object ();
 77 
78      / ** 
79       * lowest priority
 80       * / 
81      public  Final  static  int MIN_PRIORITY =. 1 ;
 82  
83      / ** 
84       * default priority
 85       * / 
86      public  Final  static  int NORM_PRIORITY =. 5 ;
 87  
88      / ** 
89       * highest priority
 90       * / 
91 is      public  Final  static  int MAX_PRIORITY = 10;

Constructor:

We look at the source code can be seen, all constructors are calling init () method; we say it init () method

1  Private  void the init (G ThreadGroup, Runnable target, String name, Long the stackSize, AccessControlContext the ACC, boolean inheritThreadLocals) {
 2      // thread name can not be empty 
3      IF (name == null ) {
 4          the throw  new new NullPointerException ( "name not CAN BE null " );
 . 5      }
 . 6      the this .name = name;
 . 7  
. 8      // threads parent thread of the current thread is created for running 
. 9      the thread parent = currentThread ();
 10  
. 11      // -------------- 安全校验 start ----------------
12     SecurityManager security = System.getSecurityManager();
13     if (g == null) {
14         if (security != null) {
15             g = security.getThreadGroup();
16         }
17 
18         if (g == null) {
19             g = parent.getThreadGroup();
20         }
21     }
22 
23     g.checkAccess();
24 
25     if (security != null ) {
 26 is          IF (isCCLOverridden (getClass ())) {
 27              security.checkPermission (SUBCLASS_IMPLEMENTATION_PERMISSION);
 28          }
 29      }
 30      // -------------- security verification End --- -------------
 31 is  
32      @ recording does not start at the number of threads 
33 is      g.addUnstarted ();
 34 is  
35      the this .group = G;
 36      // this can learn why the parent thread is a daemon thread child thread is a daemon thread 
37      the this .daemon = parent.isDaemon ();
 38      // from this we can learn why the priority is inherited from the parent of the child thread thread 
39      the this .priority = parent.getPriority();
40 
41     if (security == null || isCCLOverridden(parent.getClass())) {
42         this.contextClassLoader = parent.getContextClassLoader();
43     } else {
44         this.contextClassLoader = parent.contextClassLoader;
45     }
46 
47     this.inheritedAccessControlContext = acc != null ? acc : AccessController.getContext();
48     // 设置线程的执行体
49     this.target = target;
50     //Thread priorities set 
51 is      the setPriority (priority);
 52 is  
53 is      IF (! && parent.inheritableThreadLocals inheritThreadLocals = null ) {
 54 is          // provided that inherited from the parent thread values child thread 
55          the this .inheritableThreadLocals = ThreadLocal.createInheritedMap (parent.inheritableThreadLocals );
 56 is      }
 57 is  
58      the this .stackSize = the stackSize;
 59      // set the thread ID 
60      TID = nextThreadID ();
 61 is }

start():

. 1  public  the synchronized  void Start () {
 2      // only the active state to the thread is not started 
. 3      IF (threadStatus = 0! ) {
 . 4          the throw  new new an IllegalThreadStateException ();
 . 5      }
 . 6  
. 7      // add the current thread to the thread group 
. 8      group.add ( the this );
 . 9  
10      Boolean Started = to false ;
 . 11      the try {
 12 is          // thread is set to the ready state, the function level for the JVM 
13 is          START0 ();
 14          Started = to true ;
15      } the finally {
 16          the try {
 . 17              // not started after startup failure Threads + 1'd 
18 is              IF (! Started) {
 . 19                  group.threadStartFailed ( the this );
 20 is              }
 21 is          } the catch (the Throwable the ignore) {
 22 is  
23 is          }
 24      }
 25 }
1 private native void start0();

 run():

1  / * 
2  * specific thread execution logic
 . 3   * / 
. 4  @Override
 . 5  public  void RUN () {
 . 6      // Runnable executed if specified, the specified logical run of Runnable 
. 7      IF (target! = Null ) {
 . 8          target .run ();
 . 9      }
 10 }

Guess you like

Origin www.cnblogs.com/bzfsdr/p/11577488.html