jdk研究——java.lang
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的更多相关文章
- 深入研究java.lang.ThreadLocal类	(转)
		深入研究java.lang.ThreadLocal类 一.概述 ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是thr ... 
- 深入研究java.lang.ProcessBuilder类
		深入研究java.lang.ProcessBuilder类 一.概述 ProcessBuilder类是J2SE 1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它 ... 
- 深入研究java.lang.Process类
		一.概述 Process类是一个抽象类(所有的方法均是抽象的),封装了一个进程(即一个执行程序). Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态 ... 
- 深入研究java.lang.Runtime类【转】
		转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ... 
- centos  解压jdk安装包方式安装jdk   出现 java/lang/NoClassDefFoundError: java/lang/Object 错误
		安装完JDK ,设定环境变量后出现这个错误: [root@localhost lib]# javacError occurred during initialization of VMjava/lan ... 
- jdk之java.lang.Integer源码理解
		基本数据类型的包装类java.lang.Integer是我们频繁使用的一个系统类,那么通过一个示例反应出的几个问题来深入理解一下此类的源码. 需求:实现Integer类型的两个数值交换. packag ... 
- 深入研究java.lang.Runtime类
		一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ... 
- 深入研究java.lang.ThreadLocal类
		一.概述 ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是threadlocalvariable(线程局部变量).也许 ... 
- 【转】深入研究java.lang.Runtime类
		一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ... 
随机推荐
- 深入研究C语言 第一篇(续)
			没有读过第一篇的读者,可以点击这里,阅读深入研究C语言的第一篇. 问题一:如何打印变量的地址? 我们用取地址符&,可以取到变量的偏移地址,用DS可以取到变量的段地址. 1.全局变量: 我们看到 ... 
- adb devices 端口占用
			一. 1.通过cmd命令,输入adb devices查看连接设备时,报错 2 .通过adb nodaemon server 查看adb server绑定的端口.提示“通过每个套接字地址只能使用一次” ... 
- html5新特性之画布
			1.canvas的理解 canvas是一个矩形区域,在这个区域内,通过js可以对区域内的每一帧像素控制 2.js操作canvas对象 canvas对象.getContext("2d" ... 
- 【转】简易smtp调用类
			用PHP发邮件的一个类,无需验证SMTP主机,类代码你可以不用管,只要按照后面的使用方法操作就行,这个类要先保存成一个php文件,文件名就叫smtp.php吧,下面是该文件代码: <?php c ... 
- pragma
			在所有的预处理指令中,#pragma指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个 编译器给出了一个方法,在保持与C和C++语言完全兼容的 ... 
- redis客户端连接异常
			本文参考:http://mdba.cn/2015/04/02/redistwemproxy-%e5%ae%a2%e6%88%b7%e7%ab%af%e8%bf%9e%e6%8e%a5%e5%bc%82 ... 
- Debian系统vim中文显示乱码问题
			网上查的一堆东西好像都不灵,试了半天! 先安装中文字体:sudo aptitude install fonts-arphic-uming fonts-wqy-zenhei 然后:sudo locale ... 
- mysql注入读写文件
			mysql <5.0 读文件:load_file() sql-shell select load_file(''); d:/www/xx/index.php /home/webroot/.... ... 
- [UCSD白板题] Minimum Dot Product
			Problem Introduction The dot product of two sequences \(a_1,a_2,\cdots,a_n\) and \(b_1,b_2,\cdots,b_ ... 
- Android中处理OnClick和OnTouch方法冲突的解决方案
			目前想到的最好的解决方法,大家有更好的欢迎告知. 问题:在一个view中有一个按钮,要求可以通过点按移动这个按钮,同时单纯的点击而不移动这个按钮的话可以跳转到新的Activity. 遇到的困难:按钮的 ... 
