JDK Research--java.lang

Source: Internet
Author: User
Tags java reference

JDK Research

What does volatile mean?


How to look at the JDK source code? How to debug the source code! ---------carefully interpret key classes, key codes, common API explanations! I don't know where I'm in doubt.
--------don't know too much how to do ....


Seek to share the belt
Beg to explain the principle ah! It's better to have a teacher than not!

Key code, difficult to understand the code is what ah!

Connecting link

Structure diagram? Water diagram?


Which is the glue code, the auxiliary code

Package Java.lang

Object

System

A large number of occurrences are similar:
SecurityManager sm = getSecurityManager ();----------security mechanisms related to Java
if (SM! = null) {
Sm.checkpermission (New Runtimepermission ("getenv.") +name));
}


The system's GC calls the runtime's 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

The key: Of course it's the Start method--------synchronized
Public synchronized void Start () {
if (threadstatus! = 0)
throw new Illegalthreadstateexception ();
Group.add (this);
Start0 ();
if (Stopbeforestart) {
Stop0 (Throwablefromstop);
}
}

Private native void Start0 ();

Run Method:
Private Runnable target;
public void Run () {
if (target! = null) {
Target.run ();
}
}

So, the code that really runs is to implement the class's Run method-----Two ways to implement a custom thread: Inheriting thread and implementing runnable are essentially the same, invoking implementations, inheriting classes, starting with thread, running the Run method

And, call the Start method, just to let the thread start running, to execute the contents of the run

There are a lot of native methods like that.


The state of the thread:

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

/**
* Thread state for a runnable thread. A thread in the runnable
* Executing in the Java virtual machine
* 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 was 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 was waiting for another thread to
* Perform a particular action.
*
* For example, a thread this has called <tt>object.wait () </tt>
* On a object is waiting for another the thread to the 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 the 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 () {----------------------used less in this method ....
Get current thread state
Return Sun.misc.VM.toThreadState (Threadstatus);
}

From here you can see the status of the outgoing thread: NEW RUNNABLE BLOCKED waiting timed_waiting TERMINATED

Threadlocal<t>
The main thing is to access the collection type variables bound on the thread: threadlocals

Common 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

The essential:
Printstacktrace:
GetMessage ()
Getcause ()

is actually called by another native method to implement the
Private native int getstacktracedepth ();
Private native stacktraceelement getstacktraceelement (int index);

String
Property
Count is private, but common when debugging
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 all returns a string, and the method name should mean 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)-----called the corresponding ToString method separately
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 ();
}


A bunch of construction methods:

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)


The common Equals method
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;
}
That
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)

The difference between StringBuffer and StringBuilder? I really do not,,, this test too that!

Public final class StringBuffer
Extends Abstractstringbuilder
Implements Java.io.Serializable, Charsequence
Main 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)

Main API
Just like the above,
But no reverse ()

replace (int, int, String)


It is not safe to use an instance of StringBuilder for multiple threads. If such synchronization is required, it is recommended to use StringBuffer.
-----Just the difference here! , indeed, found that StringBuilder and stringbuffer most of the method signature implementation, such as exactly, just StringBuffer method each front has synchronized!!

Public
Class Stackoverflowerror extends Virtualmachineerror {
Stackoverflowerror ()
Stackoverflowerror (String)


Basically similar wrapper class, mostly static method, Java implementation, non-native implementation

Exception is a simple class that provides only a few construction methods.

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


Deprecated is an annotated interface, D uppercase
@Documented
@Retention (Retentionpolicy.runtime)
Public @interface Deprecated {
}


Public final class Compiler {----------internal mechanism
Command (Object)
Compileclass (class<?>)
Compileclasses (String)
Disable ()
Enable ()
Initialize ()
Registernatives ()
Compiler ()


Public abstract class ClassLoader {-------is a very important class, with some internal implementations

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 is an internal implementation----------end with 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 {----Why?

JAVA.LANG.REF------Java Reference

Java.lang.reflect-----Java Reflection

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 Research--java.lang

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.