JDK8 source code reading (a) java.util.Object

First, class
1.1 class modifier


A. Object is the parent of all classes.

B. All objects, including arrays are implemented, including the method of the Object class.

 

1.2 class structure in FIG.


 

Second, the field
1.1 field list
no.

 

Third, the method
3.1 Methods List


NOTE: As shown above, a total of 12 methods. Further comprising a static block. 

        Green On behalf of the lock is public, red shut representatives are private, protected gray key represents.

3.2 Method Modifiers

 

 

 


3.3 Detailed methods
3.3.0 [static] block

 

 

 

Figure: is a simple call to a bit registerNatives method, initialize several other native methods.

 

3.3.1 Constructors
default no-argument constructor.

Note: If you do not what to do in the constructor, then the method will be omitted to write, such as Object. If you have special treatment, it will write out manually, such as ArrayList.

 

3.3.2 registerNatives()

 

 

 


A. First of all native modified method shows that implementation of the method is not in the JDK, but implemented by the C / C ++ in the operating system, and compiled into .dll files from java to call. For different platforms, the specific implementation should be different.

B. Internet looking for a bit, which is the main method for several local methods in this class to register as hashCode, getClass, etc., that is java initialization method mapped to the C's.

 

3.3.3 getClass()

 

 

 


A. Returns the runtime class of this object. Returns the class object is locked by static synchronized methods represent classes of objects.

B. Example .getClass () returns the results of the call and return the same results Class.class, as an example:

 

 

 

 

 

3.3.4 hashCode()

 

 

 


A. Return the hash value of the object, which support the achievement of these HashMap hash table like.

B. Whenever the process JAVA applications executing multiple calls on the same subject, as long as no modification equals method, then the method must return the same hash code.

C. Another one execution of an application to the same application from the hash value does not necessarily coincide.

D. If the two objects are compared both are equal according to the equals method, the calling object maybe hashCode method must obtain the same hash code; if the two objects are not equal for comparing the two according to the equals method, maybe a Object calling the hashCode method to get the hash code may be different. In this case, the two are not necessarily linked. However, the programmer should be aware that produce distinct integer results may improve the performance of the hash table for different objects.

E. as is reasonably practicable, hashcode for class object definitions will not return different integers for different objects. (This is usually accomplished by converting the internal address of the object into an integer, but this implementation technique is not required for the Java programming language.)

 

3.3.5 equals()

 

 

 


A. indicate whether other object is "equal to" this object.

B. Please note that whenever this method is overridden, usually need to override hashCode methods in order to maintain the general contract of hashCode method, which is equal to the declared objects must have equal hash value.

 

3.3.6 clone

 

 

 


A. Creates and returns a copy of this object. The method performs a "shallow copy", rather than "deep copy." This method will create a new instance of the object class, and use the contents of the field corresponding to the object to initialize all fields, as by assignment, the contents of these fields themselves are not clones;

B. By convention, the subject cloning method returns should be independent. To achieve this independence, before returning, you may be required () method of changing one or more fields of the object by super.clone. Typically, this means that if you want to copy internal object references to a number of variable objects, then the object is returned after replication, variable references to these objects should be pointing these variable copy of the object;

C. If a class does not implement the Cloneable interface will throw a CloneNotSupportedException; Object class itself does not implement the interface Cloneable, so calling the clone method of the Object class if it is, it will cause an exception at run time; all array types are Cloneable interface that has been achieved, and an array T [] call the method return type is an array T [], where, T is an arbitrary reference or a primitive data type.

 

3.3.7 toString

 

 

 


A. Returns a string representation of the object. The string consists of: type the full name + @ + hash value of the object of unsigned hexadecimal composition.

B. follows:

 

 

 

 

 

3.3.8 notify()

 

 

 


A. activate waiting for this object's monitor a single thread. If any threads are waiting for this object, choose to wake up one thread. Choice is arbitrary, by the perpetrators of discretion. Wherein a thread by calling the object wait method to wait for a monitor;

B. lock before giving up on this subject in the current thread, awakened thread will not continue. Does not enjoy reliable privilege or disadvantage for example, wake up the next thread in the lock of the object becomes the thread; the thread will wake up in the usual manner with any other threads may compete actively competing to synchronize on this object;

C. This method can only be called by the owner of the thread object's monitor. Thread becomes the owner of the object's monitor in one of three ways: 

     . C1 synchronization by performing the method of the object instance;

     . C2 magnet synchronous synchronized statement by performing on the object;

     . C3 for the object type Class static method by performing synchronization class;

D. Only one monitor thread that owns the object;

 

3.3.9 notifyAll()

 

 

 


A. wake up all the threads are waiting for this object's monitor. A thread waits on the wait method by calling the object wherein the monitor;

B. lock before giving up on this subject in the current thread, awakened thread will not continue. Does not enjoy reliable privilege or disadvantage for example, it wakes up the next thread thread of the object to be locked in; the thread will wake up in the usual manner with any other threads may compete actively competing to synchronize on this object;

C. This method can only be called by the owner of the thread object's monitor. The thread to be described in the way the owner of the monitor, please refer to the notify method;

 

3.3.10 wait(long timeout)

 

 

 


A. the current thread to wait until the call or notify method for this object notifyall method, or the specified time has passed another thread;

B. The current thread must own this object's monitor;

C. This method allows the current thread itself into the wait set for this object, and then give up all sync statement on this subject. For thread scheduling purposes, the thread will be disabled, and lies dormant until one of four things happens: 
      c1 Some other thread invokes the notify method for this object, and the thread happens to be arbitrarily chosen to be awakened thread;.

      . C2 Some other thread invokes notifyall method for this object;

      . C3 Other methods thread interrupt terminal of the current thread;

      c4. specified real-time has elapsed, more or less. However, if the timeout is zero, it is not considered real-time, a thread just waiting to be notified;

And then wait for the thread to Remove from the object, and re-enable thread scheduling. Then, it is right in the usual way synchronization with other threads compete on the object; once it gained control of the object, it all synchronized statement on the object will be restored to its current state. In other words, in the case when it calls the wait method. Then thread returns from the call wait approach. Thus, return from the wait method, the synchronization state of the objects and threads call wait state when exactly the same way;

D. threads may not be notified, interrupted, or in the case of a timeout wake, which is called the false wake. Although this rarely happens in practice, but the application must lead to conditions to be awakened by a thread tests to guard against this case, and if the condition is not satisfied, continue to wait. In other words, waits should always appear in a circular form;

E. If the current thread while waiting or waiting to be interrupted before any thread, thrown InterruptedException. Prior to reducing the locked state of the object in accordance with the above description, this does not throw exception;

F. Please note, wait method when the current thread into the wait set for the object, the object is only unlocked; when a thread is waiting for, any other object can synchronize the current thread will remain locked.

G. This method can only be called by the owner of the thread object's monitor. For a description of the way the thread becomes the owner of the monitor, see the notify method.

 

 

3.3.11 wait(long timeout, int nanos)

 

 

 

 

 

 


A. the current thread to wait until another thread calls notify method or notifyall method for this object, or some other thread interrupts the current thread, or after a certain real-time;

Method B. This method is similar to a wait parameter, but it allows better control of the waiting time notification before giving up. Real-time in nanoseconds is expressed as: 1000000 * timeout + nanos (ns). In all other respects, this method is a method parameter wait (long) perform the same operation. In particular, the same wait (0,0) and wait (0) meaning;

C. The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following occurs:

     . C1 another thread by calling notify or notifyall method notifies waiting threads wake of this object's monitor;

     C2. timeout period (specified by the timeout parameter milliseconds plus nanos nanoseconds) has elapsed.

    The thread then waits until it can re-obtain ownership of the monitor and continue;

D. single argument version, interrupts and spurious wake-up is possible, and the method should always be used in a loop;

E. This method can only be called by the owner of the thread object's monitor. The thread to be described in the way the owner of the monitor, please refer to the notify method;

 

3.3.12 public final void wait()

 

 

 


A. the current thread to wait until another thread calls notify method or notifyall method for this object. In other words, the behavior of this method as if it were just calling wait (0) method;

B. The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread in this object's monitor by calling the notify method or methods to inform notifyall wait wake. The thread then waits until it can re-obtain ownership of the monitor and continue.

C. and single-parameter version, interrupts and spurious wake-up is possible, and the method should always be used in a loop;

D. This method can only be called by the owner of the thread object's monitor. The thread to be described in the way the owner of the monitor, please refer to the notify method;

 

3.3.13 finalize()

 


A. When the garbage collector determines there are no references to objects, called by the garbage collector on an object. Subclasses override the Finalize method to release system resources or perform other cleanup;

B. usually finalize the agreed: if the Java virtual machine has determined that there is no longer any means of access to the object by any thread has not died, it is invoked, unless the operator due to the termination of some other object or class taken, and the object or class is ready finalized. Finalize method can take any action, including the object again so that other threads may be used; however, the general purpose is to perform the Finalize cleanup operations before the object is irrevocably discarded;

C. Object class object finalize method of any special operation, it just returns to normal. Object subclass may override this definition;

D. Java programming language does not guarantee which thread will invoke the finalize method for any given object. However, it is guaranteed to be called finalize thread when calling finalize will not be holding any user-visible synchronization locks. If the Finalize method throws an uncaught exception, the exception is ignored and terminate Finalize the object;

E. After the finalize method has been invoked as an object, no further action is taken until the Java virtual machine determines again there is no longer any way to access the object has not been accessed by any thread, including other object or class may be ready operation, the object may be discarded at this time.

F. For any given object, Java virtual machine will not be calling finalize method multiple times;

Any exception G. Finalize Finalize method throws led to the termination of this object, but otherwise ignored.

 

 



Note: This year we JDK some of the basic classes of source sorted out, we welcome correction and discussion.

         If you have the wrong place, welcome to correct me teacher.

         If you feel good, like a point, then I would be very grateful, thank you ~

         Reproduced, please indicate the link to the source.
----------------
Disclaimer: This article is CSDN blogger "Please call me uncle apes' original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_35206261/article/details/88365893

Guess you like

Origin www.cnblogs.com/spring-cloud/p/11432620.html