1. 程式人生 > >Java Thread 原始碼 附錄

Java Thread 原始碼 附錄

1 /* 2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 26 package java.lang; 28 import java.lang.ref.Reference; 29 import java.lang.ref.ReferenceQueue; 30 import java.lang.ref.WeakReference; 31 import java.security.AccessController; 32 import java.security.AccessControlContext; 33 import java.security.PrivilegedAction; 34 import java.util.Map; 35 import java.util.HashMap; 36 import java.util.concurrent.ConcurrentHashMap; 37 import java.util.concurrent.ConcurrentMap; 38 import java.util.concurrent.locks.LockSupport; 39 import sun.nio.ch.Interruptible; 40 import sun.reflect.CallerSensitive; 41 import sun.reflect.Reflection; 42 import sun.security.util.SecurityConstants; 140 public 141 class Thread implements Runnable { 142 /* Make sure registerNatives is the first thing <clinit> does. */ 143 private static native void registerNatives(); 144 static { 145 registerNatives(); 146 } 148 private volatile char name[]; 149 private int priority; 150 private Thread threadQ; 151 private long eetop; 153 /* Whether or not to single_step this thread. */ 154 private boolean single_step; 156 /* Whether or not the thread is a daemon thread. */ 157 private boolean daemon = false; 159 /* JVM state */ 160 private boolean stillborn = false; 162 /* What will be run. */ 163 private Runnable target; 165 /* The group of this thread */ 166 private ThreadGroup group; 168 /* The context ClassLoader for this thread */ 169 private ClassLoader contextClassLoader; 171 /* The inherited AccessControlContext of this thread */ 172 private AccessControlContext inheritedAccessControlContext; 174 /* For autonumbering anonymous threads. */ 175 private static int threadInitNumber; 176 private static synchronized int nextThreadNum() { 177 return ++; 178 } 180 /* ThreadLocal values pertaining to this thread. This map is maintained 181 * by the ThreadLocal class. */ 182 ThreadLocal.ThreadLocalMap threadLocals = null; 184 /* 185 * InheritableThreadLocal values pertaining to this thread. This map is 186 * maintained by the InheritableThreadLocal class. 187 */ 188 ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; 190 /* 191 * The requested stack size for this thread, or 0 if the creator did 192 * not specify a stack size. It is up to the VM to do whatever it 193 * likes with this number; some VMs will ignore it. 194 */ 195 private long stackSize; 197 /* 198 * JVM-private state that persists after native thread termination. 199 */ 200 private long nativeParkEventPointer; 202 /* 203 * Thread ID 204 */ 205 private long tid; 207 /* For generating thread ID */ 208 private static long threadSeqNumber; 210 /* Java thread status for tools, 211 * initialized to indicate thread 'not yet started' 212 */ 214 private volatile int threadStatus = 0; 217 private static synchronized long nextThreadID() { 218 return ++; 219 } 227 volatile Object parkBlocker; 229 /* The object in which this thread is blocked in an interruptible I/O 230 * operation, if any. The blocker's interrupt method should be invoked 231 * after setting this thread's interrupt status. 232 */ 233 private volatile Interruptible blocker; 234 private final Object blockerLock = new Object(); 236 /* Set the blocker field; invoked via sun.misc.SharedSecrets from java.nio code 237 */ 238 void blockedOn(Interruptible b) { 239 synchronized () { 240 = b; 241 } 242 } 247 public final static int MIN_PRIORITY = 1; 252 public final static int NORM_PRIORITY = 5; 257 public final static int MAX_PRIORITY = 10; 264 public static native Thread currentThread(); 282 public static native void yield(); 301 public static native void sleep(long millis) throws InterruptedException; 325 public static void sleep(long millis, int nanos) 326 throws InterruptedException { 327 if (millis < 0) { 328 throw new IllegalArgumentException("timeout value is negative"); 329 } 331 if (nanos < 0 || nanos > 999999) { 332 throw new IllegalArgumentException( 333 "nanosecond timeout value out of range"); 334 } 336 if (nanos >= 500000 || (nanos != 0 && millis == 0)) { 337 millis++; 338 } 340 sleep(millis); 341 } 347 private void init(ThreadGroup g, Runnable target, String name, 348 long stackSize) { 349 init(g, target, name, stackSize, null); 350 } 363 private void init(ThreadGroup g, Runnable target, String name, 364 long stackSize, AccessControlContext acc) { 365 if (name == null) { 366 throw new NullPointerException("name cannot be null"); 367 } 369 Thread parent = currentThread(); 370 SecurityManager security = System.getSecurityManager(); 371 if (g == null) { 372 /* Determine if it's an applet or not */ 374 /* If there is a security manager, ask the security manager 375 what to do. */ 376 if (security != null) { 377 g = security.getThreadGroup(); 378 } 380 /* If the security doesn't have a strong opinion of the matter 381 use the parent thread group. */ 382 if (g == null) { 383 g = parent.getThreadGroup(); 384 } 385 } 387 /* checkAccess regardless of whether or not threadgroup is 388 explicitly passed in. */ 389 g.checkAccess(); 391 /* 392 * Do we have the required permissions? 393 */ 394 if (security != null) { 395 if (isCCLOverridden(getClass())) { 396 security.checkPermission(); 397 } 398 } 400 g.addUnstarted(); 402 this. = g; 403 this. = parent.isDaemon(); 404 this. = parent.getPriority(); 405 this. = name.toCharArray(); 406 if (security == null || isCCLOverridden(parent.getClass())) 407 this. = parent.getContextClassLoader(); 408 else 409 this. = parent.contextClassLoader; 410 this. = 411 acc != null ? acc : AccessController.getContext(); 412 this. = target; 413 setPriority(); 414 if (parent.inheritableThreadLocals != null) 415 this. = 416 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); 417 /* Stash the specified stack size in case the VM cares */ 418 this. = stackSize; 420 /* Set thread ID */ 421 = nextThreadID(); 422 } 431 @Override 432 protected Object clone() throws CloneNotSupportedException { 433 throw new CloneNotSupportedException(); 434 } 443 public Thread() { 444 init(null, null, "Thread-" +

相關推薦

Java Thread 原始碼 附錄

1 /* 2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER

Java Thread 原始碼解析

Thread 原始碼解析 執行緒的方法大部分都是使用Native使用,不允許應用層修改,是CPU排程的最基本單元。執行緒的資源開銷相對於程序的開銷是相對較少的,所以我們一般建立執行緒執行,而不是程序執行。 Thread 構造方法 /** * In

Java ThreadLocal原始碼解析: Thread和ThreadLocal

之前對TreadLocal有所理解,對原理也有所瞭解,但一直不深入,重新整理,希望藉以加深理解和印象。 在Jdk1.8中,ThreadLocal相關程式碼主要分為三部分: Thread,其中Thread中儲存對ThreadLocal.ThreadLocalMap的引用,作為T

Java thread dump分析找到耗費CPU最高的原始碼

第二步:執行kill -3 pid獲取thread dump日誌(pid就是第一步獲取到的)。注意:在不同的linux環境下執行輸出的日誌的地方可能不同。在IBM的PowerPC小型機上的linux上執行kill -3 pid會在工作目錄下產生類似javacore.20100409.161739.7614.0

Java原始碼Thread

前言 JDK版本:1.8 閱讀了Object的原始碼,wait和notify方法與執行緒聯絡緊密,而且多執行緒已經是必備知識,那保持習慣,就從多執行緒的源頭Thread類開始讀起吧。由於該類比較長,只讀重要部分 原始碼 類宣告和重要屬性 package java.lang; public class T

各種 Java Thread State【轉載】

tin run 監視器 .com str ack tran queue 我們 1,線程狀態為“waiting for monitor entry”: 意味著它 在等待進入一個臨界區 ,所以它在”Entry Set“隊列中等待。 此時線程狀態一般都是 Blocked: j

Java - Thread 和 Runnable實現多線程

abs java jdk1 public adt system 轉載 作用 final Java多線程系列--“基礎篇”02之 常用的實現多線程的兩種方式 概要 本章,我們學習“常用的實現多線程的2種方式”:Thread 和 Runnable。之所以說是常用的,是因為通

JAVA Thread中的狀態

mac star java started class out bject can follow /** * A thread state. A thread can be in one of the following states: * <

Java Thread中,run方法和start方法的區別

bsp 區別 繼續 時間片 ron thread類 等待 nbsp art 兩種方法的區別: 1.start方法 用 start方法來啟動線程,是真正實現了多線程, 通過調用Thread類的start()方法來啟動一個線程,這時此線程處於就緒

Java-Thread-生產者消費模式

ide 沒有 john lean pos con clas catch this package com.cn.thread;import java.util.ArrayList;import java.util.List;/** * 模擬 生產者消費模式 * @autho

Java Thread系列(十)Future 模式

業務 throw 而且 代碼 ack void rri 是不是 決定 Java Thread系列(十)Future 模式 Future 模式適合在處理很耗時的業務邏輯時進行使用,可以有效的減少系統的響應時間,提高系統的吞吐量。 一、Future 模式核心思想 如下的請求

Java Thread系列(五)synchronized

執行 java 釋放 lock java t 操作 bject 線程 出現 Java Thread系列(五)synchronized synchronized鎖重入 關鍵字 synchronized 擁有鎖重入的功能,也就是在使用 synchronized 時,當線程等到

Java Thread系列(二)線程狀態

做的 tor throws 前臺 bject 線程休眠 enume 死鎖 做出 Java Thread系列(二)線程狀態 一、線程的五種狀態 新建狀態(New):新創建了一個線程對象,尚未啟動。 就緒狀態(Runnable):也叫可運行狀態。線程對象創建後,其他線程調用

Java Thread系列(九)Master-Worker模式

線程 支持 需要 列表 deque 開啟 exc oid src Java Thread系列(九)Master-Worker模式 Master-Worker模式是常用的並行設計模式. 一、Master-Worker 模式核心思想 Master-Worker 系統由兩個角

Java Thread系列(一)線程創建

nds list www imp tor 所有 clas HR dex Java Thread系列(一)線程創建 Java 中創建線程主要有三種方式:繼承 Thread、實現 Runnable 接口、使用 ExecutorService、Callable、Future 實現

Java Thread系列(三)線程安全

AI 資源 習慣 get string tar rup end 就是 Java Thread系列(三)線程安全 一、什麽是線程安全 線程安全概念:當多個線程訪問某一個類(對象或方法)時,這個類始終都能表現出正確的行為,那麽這個類(對象或方法)就是線程安全的。 線程安全來

Java Thread系列(四)線程通信

ice 集合 就是 int name 發出 max 執行 生產 Java Thread系列(四)線程通信 一、傳統通信 public static void main(String[] args) { //volatile實現兩個線程間數據可見性 priva

ThreadPoolExecutor – Java Thread Pool Example

limit interface cat rejected worker cif tin interval getname https://www.journaldev.com/1069/threadpoolexecutor-java-thread-pool-example-

性能分析之-- JAVA Thread Dump 分析綜述

頻繁 之前 監控 平臺 released 端口 16進制 後來 code https://blog.csdn.net/rachel_luo/article/details/8920596 最近在做性能測試,需要對線程堆棧進行分析,在網上收集了一些資料,學習完後,將相關知識整

zbb20180913 java thread 死鎖示例代碼

exce stat pre ace imp exc @override runnable arp package com.zbb.test.thread; public class DeadLock { public static String obj1 = "obj