首页 > 代码库 > jdk研究——java.lang
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