jdk研究

volatile 是什么意思?

如何看jdk源码? 如何调试源码!---------仔细解读关键类,关键代码,常用的api的解释! 自己有疑问的不懂地方
-------- 不懂的太多怎么办。。。。

求分享求带
求讲解原理啊! 有老师还是比没有好得多!

关键代码、难懂代码是哪些啊!

承上启下

结构图?流水图?

哪些又是胶水代码呢、辅助代码

PACKAGE java.lang

Object

System

大量出现类似:
SecurityManager sm = getSecurityManager();---------- 涉及到java的安全机制
if (sm != null) {
sm.checkPermission(new RuntimePermission("getenv."+name));
}

System的gc调用的是 Runtime的gc:

public static void gc() {
Runtime.getRuntime().gc();
}

public static void runFinalization() {
Runtime.getRuntime().runFinalization();
}

public static void load(String filename) {
Runtime.getRuntime().load0(getCallerClass(), filename);
}

Thread implements Runnable

关键:当然就是它的 start方法-------- synchronized
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}

private native void start0();

run方法:
private Runnable target;
public void run() {
if (target != null) {
target.run();
}
}

所以,真正运行的代码体在于实现类的run方法----- 实现自定义线程的两种方法:继承Thread和实现Runnable本质上是一样的,都是调用实现、继承类的 ,由Thread来启动,运行run方法

而,调用start方法,只是表明让这个Thread开始运行,去执行run里面内容

还有类似很多的native方法

线程的状态:

public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,

/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,

/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,

/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,

/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,

/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}

public State getState() {---------------------- 在这个方法用得少。。。。
// get current thread state
return sun.misc.VM.toThreadState(threadStatus);
}

从这里可以看得出线程的状态: NEW RUNNABLE BLOCKED WAITING TIMED_WAITING TERMINATED

ThreadLocal<T>
主要就是访问 绑定在Thread上的集合类型变量:threadLocals

常用的api: get、set。。。

public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}

public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}

ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}

ThreadLocal.ThreadLocalMap threadLocals = null;

Throwable

关键:
printStackTrace:
getMessage()
getCause()

其实是调用另外的native方法实现的
private native int getStackTraceDepth();
private native StackTraceElement getStackTraceElement(int index);

String
属性
count 私有,但是调试的时候常见
offset
private int hash; // Default to 0
char value[]

format(String, Object...)
public static String format(String format, Object ... args) {
return new Formatter().format(format, args).toString();
}
valueOf 全返回string,方法名的意思应该是 string value of an char/int/.. variable
valueOf(char)
char data[] = {c};
return new String(0, 1, data);

valueOf(char[])
valueOf(char[], int, int)
valueOf(double)
valueOf(float)
valueOf(int) ----- 分别调用了相应的tostring方法
return Integer.toString(i, 10);
valueOf(long)
return Long.toString(l, 10);
valueOf(Object)

public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}

一堆的构造方法:

String()
String(byte[])
String(byte[], Charset)
String(byte[], int)
String(byte[], int, int)
String(byte[], int, int, Charset)
String(byte[], int, int, int)
String(byte[], int, int, String)
String(byte[], String)
String(char[])
String(char[], int, int)
String(int, int, char[])
String(int[], int, int)
String(String)
String(StringBuffer)
String(StringBuilder)

常用的 equals 方法
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
int n = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;

if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}

public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;

for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
即:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

public String substring(int beginIndex, int endIndex) {
replace(char, char)
replace(CharSequence, CharSequence)
replaceAll(String, String)
replaceFirst(String, String)

StringBuffer 和 StringBuilder 的区别? 我真不见得,,, 这考得太那个了吧!

public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
主要api

StringBuffer()
StringBuffer(CharSequence)
StringBuffer(int)
StringBuffer(String)

append(char[])
append(char[], int, int)
append(CharSequence)
append(CharSequence, int, int)
append(double)
append(float)
append(int)
append(long)
append(Object)
append(String)
append(StringBuffer)
appendCodePoint(int)

reverse()

insert......

public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequenceappend(char)

主要api
跟上面的一样,
不过没有reverse()

replace(int, int, String)

将 StringBuilder 的实例用于多个线程是不安全的。如果需要这样的同步,则建议使用 StringBuffer。
----- 仅仅是这儿区别吧!,确实,发现StringBuilder和StringBuffer大部分的方法签名实现等完全一样,只是StringBuffer的方法每个前面都有synchronized !!

public
class StackOverflowError extends VirtualMachineError {
StackOverflowError()
StackOverflowError(String)

基本类似的包装类,大都是静态的方法,java实现,非native实现

Exception 是一个简单的类,仅仅只是提供了几个构造方法而已

Exception()
Exception(String)
Exception(String, Throwable)
Exception(Throwable)

Deprecated 是一个注解的接口,D大写
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Deprecated {
}

public final class Compiler {---------- 内部机制
command(Object)
compileClass(Class<?>)
compileClasses(String)
disable()
enable()
initialize()
registerNatives()
Compiler()

public abstract class ClassLoader {------- 是一个很重要的类,有一些内部实现

protected synchronized Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
// First, check if the class has already been loaded
Class c = findLoadedClass(name);
if (c == null) {
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClass0(name);
}
} catch (ClassNotFoundException e) {
// If still not found, then invoke findClass in order
// to find the class.
c = findClass(name);
}
}
if (resolve) {
resolveClass(c);
}
return c;
}

public final
class Class<T> implements java.io.Serializable,
java.lang.reflect.GenericDeclaration,
java.lang.reflect.Type,
java.lang.reflect.AnnotatedElement {

public static Class<?> forName(String className)
throws ClassNotFoundException {
return forName0(className, true, ClassLoader.getCallerClassLoader());
}

forName0 是内部实现---------- 以0结尾都这样???

======== java.lang.annotation ========
Annotation.java
AnnotationFormatError.java
AnnotationTypeMismatchException.java
Documented.java
ElementType.java
IncompleteAnnotationException.java
Inherited.java
Retention.java
RetentionPolicy.java
Target.java

java.lang.instrument
public final class ClassDefinition { ---- 干嘛的?

java.lang.ref ------ java引用

java.lang.reflect ----- java反射

package java.io

public class File
implements Serializable, Comparable<File>

abstract class FileSystem {

abstract class FileSystem {

/**
* Return the FileSystem object representing this platform's local
* filesystem.
*/
public static native FileSystem getFileSystem();

jdk研究——java.lang的更多相关文章

  1. 深入研究java.lang.ThreadLocal类 (转)

    深入研究java.lang.ThreadLocal类     一.概述   ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是thr ...

  2. 深入研究java.lang.ProcessBuilder类

     深入研究java.lang.ProcessBuilder类 一.概述       ProcessBuilder类是J2SE 1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它 ...

  3. 深入研究java.lang.Process类

    一.概述 Process类是一个抽象类(所有的方法均是抽象的),封装了一个进程(即一个执行程序).       Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态 ...

  4. 深入研究java.lang.Runtime类【转】

    转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...

  5. centos 解压jdk安装包方式安装jdk 出现 java/lang/NoClassDefFoundError: java/lang/Object 错误

    安装完JDK ,设定环境变量后出现这个错误: [root@localhost lib]# javacError occurred during initialization of VMjava/lan ...

  6. jdk之java.lang.Integer源码理解

    基本数据类型的包装类java.lang.Integer是我们频繁使用的一个系统类,那么通过一个示例反应出的几个问题来深入理解一下此类的源码. 需求:实现Integer类型的两个数值交换. packag ...

  7. 深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

  8. 深入研究java.lang.ThreadLocal类

        一.概述   ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也许 ...

  9. 【转】深入研究java.lang.Runtime类

    一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象, ...

随机推荐

  1. javascript里的继承

    js里面继承的方式, 1. 类式继承,通过构造函数的继承 function extend(subClass, superClass){ var F = function(){}; F.prototyp ...

  2. C#语言基础— 输入与输出

    C#语言基础— 输入与输出 1.1函数的四要素:名称.输入.输出.加工 1.2主函数:输出语句.输入语句: Static viod Main(string[] stgs)//下划线部分可以自己指定 { ...

  3. leetcode problem sum

    2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  4. hdu 1342(DFS)

    Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. Block循环引用问题研究

    自从苹果在objc中添加Block功能支持以后已经过了很久.目前网上对于Block的使用有很多介绍.不过对于Block的内存管理问题,则是众说纷纭.再加上objc开始使用ARC以后,对于Block的内 ...

  6. redis迁移工具-redis-migrate-tool使用测试

    https://github.com/vipshop/redis-migrate-tool一.安装redis-migrate-tool a.下载redis-migrate-tool软件包 https: ...

  7. Emgu CV播放视频

    public partial class Form1 : Form {     Capture _capture;     public Form1()     {         Initializ ...

  8. 搜索引擎 ElasticSearch 之 步步为营2 【基础概念】

    在正式学习 ElasticSearch 之前,首先看一下 ElasticSearch 中的基本概念. 这些概念将在以后的章节中出现多次,所以花15分钟理解一下是非常值得的. 英文好的同学,请直接移步官 ...

  9. [LeetCode]447 Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  10. PYTHON学习之路_PYTHON基础(6)

    学习内容: Python模块介绍 1.time &datetime模块 2.random 3.shutil 4.shelve 5.xml处理 6.configparser 7.hashlib ...