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对象, ...
随机推荐
- mysql解压缩安装(一)
MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C:\P ...
- css3之多列
- 17.iOS App设置icon,启动图,App名称的方法
icon:选择Assets-->AppIcon-->将各种尺寸的icon拖拽到相应的框中. APP名称:选择info-->Bundle name,修改APP名字. 启动图: 首先点击 ...
- Ubuntu-14.04.3下SDL2测试
最近突然蛋痛想入门Linux,想写个跨平台的游戏,各种坑,备忘,稍微记点笔记. 主要参考这个文章 buntu14.04下C++开发SDL2应用 下载与安装 到http://www.libsdl.org ...
- JS 前端格式化JSON字符串工具
JSON格式化工具,简易实现.作为技术宅,直接上代码,供大家使用.前提:一定要引入jquery哦. <!DOCTYPE html> <html lang="en" ...
- 将excel数据导入到mysql的方法
文本框被键盘遮挡到了,不会再获取焦点的时候被顶到键盘顶部.解决方案:设置A的Position为绝对定位absolute即可,其他几种定位方式未测试,但是不能是fixed ,正是因为这种定位方式,导致它 ...
- 20151012 C# 第一篇 字符与字符串
20151012 字符与字符串: Char.String等类来表示 字符类Char 1. 字符类Char 表示一个 Unicode 字符,(Unicode字符是计算机通用的字符编码,对不同语言中的每个 ...
- Android应用:StatusBar状态栏、NavigationBar虚拟按键栏、ActionBar标题栏、Window屏幕内容区域等的宽高
一.屏幕中各种栏目以及屏幕的尺寸 当我们需要计算屏幕中一些元素的高度时,或许需要先获取到屏幕或者各种栏目的高度,下面这个类包含了Status bar状态栏,Navigation bar虚拟按键栏,Ac ...
- C#与Swift异步操作的差异
作为一个从C#转到Swift的小菜鸡...最近做一个简单的请求API解析Json数据的小程序上碰到一堆小问题.尤其是在异步请求的时候,用惯了C#的async/await写法,在写Swift的时候也按着 ...
- 一款免费支持PDF、word、excel、PPT、jpeg之间互转线上软件
偶然发现的一款免费支持PDF.word.excel.PPT.jpeg之间互转,支持合并pdf.加密解密PDF的线上软件,首先声明,不是广告党,我自己试用过,确实是目前我用过最好用的,如果有朋友有更好的 ...