JDK核心JAVA源码解析(1)

想写这个系列很久了,对自己也是个总结与提高。原来在学JAVA时,那些JAVA入门书籍会告诉你一些规律还有法则,但是用的时候我们一般很难想起来,因为我们用的少并且不知道为什么。知其所以然方能印象深刻并学以致用。
首先我们从所有类的父类Object开始:

1. Object类

(1)hashCode方法和equals方法

1public native int hashCode(); 2public boolean equals(Object obj) { 3 return (this == obj); 4}

Java内规定,hashCode方法的结果需要与equals方法一致。也就是说,如果两个对象的hashCode相同,那么两个对象调用equals方法的结果需要一致。那么也就是在以后的java程序设计中,你需要同时覆盖这两个方法来保证一致性。
在Object代码中,hashCode是native的,非java代码实现。主要原因是它的实现方法是通过将对象在内存中所处于的位置转换成数字,这个数字就是hashCode。但是这个内存地址实际上java程序并不关心也是不可知的。这个地址是由JVM维护并保存的,所以实现是native的。
如果两个Object的hashCode一样,那么就代表两个Object的内存地址一样,实际上他们就是同一个对象。所以,Object的equals实现就是看两个对象指针是否相等(是否是同一个对象)
在JAVA程序设计中,对于hashCode方法需要满足:

**1.在程序运行过程中,同一个对象的hashCode无论执行多少次都要保持一致。**但是,在程序重启后同一个对象的hashCode不用和之前那次运行的hashCode保持一致。但是考虑如果在分布式的情况下,如果对象作为key,最好还是保证无论在哪台机器上运行多少次,重启多少次,不同机器上,同一个对象(指的是两个equals对象),的hashCode值都一样(原因之后会说的)。
例如这里的Object对于hashCode的实现,在当前次运行,这个对象的存储地址是不变的。所以hashCode不变,但是程序重启后就不一定了。对于String的hashCode实现:

1public int hashCode() { 2 int h = hash; 3 if (h == 0 && value.length > 0) { 4 char val[] = value; 5 6 for (int i = 0; i < value.length; i++) { 7 h = 31 * h + val[i]; 8 } 9 hash = h; 10 } 11 return h; 12}

String就是一种典型的适合在分布式的情况下作为key的存储对象。无论程序何时在哪里运行,同一个String的hashCode结果都是一样的。
2.如果两个对象是euqal的,那么hashCode要相同
**3.建议但不强制对于不相等的对象的hashCode一定要不同。**这样可以有效减少hash冲突
hashCode主要用于集合类中的hashmap(hashset的底层实现也是hashmap)。之后我们在Set源代码部分会继续细说。

对于equals方法的实现,则需要满足:
**1. 自反性:**对于任意非null的x,x.equals(x)为true
**2. 对称性:**对于任意非null的x和y,如果x.equals(y),那么y.equals(x)
**3. 传递性:**对于任意非null的x,y和z,如果x.equals(y)并且y.equals(z),那么x.equals(z)
**4. 一致性:**对于任意非null的x和y,无论执行多少次x.equals(y)结果都是一样的
5. 对于任意非null的x,x.equals(null)返回false

自己实现这些方法一定要满足这些条件,否则再用Java其他数据结构时会有意想不到的结果,后面会在回顾这个问题。

(2)toString( )方法——打印对象的信息

这个没啥好说的,默认的实现就是class名字@hashcode(如果hashCode()方法也是默认的话,那么就是如之前所述地址)

1public String toString() { 2 return getClass().getName()+"@"+Integer.toHexString(hashCode()); 3}

(3) wait(), notify(), notifyAll()

这些属于基本的Java多线程同步类的API,都是native实现:

1public final native void wait(long timeout) throws InterruptedException; 2public final native void notify(); 3public final native void notifyAll();

那么底层实现是怎么回事呢?
首先我们需要先明确JDK底层实现共享内存锁的基本机制。
每个Object都有一个ObjectMonitor,这个ObjectMonitor中包含三个特殊的数据结构,分别是CXQ(实际上是Contention List),EntryList还有WaitSet;一个线程在同一时间只会出现在他们三个中的一个中。首先来看下CXQ:
这里写图片描述
一个尝试获取Object锁的线程,如果首次尝试(就是尝试CAS更新轻量锁)失败,那么会进入CXQ;进入的方法就是CAS更新CXQ指针指向自己,如果成功,自己的next指向剩余队列;CXQ是一个LIFO队列,设计成LIFO主要是为了:
1. 进入CXQ队列后,每个线程先进入一段时间的spin自旋状态,尝试获取锁,获取失败的话则进入park状态。这个自旋的意义在于,假设锁的hold时间非常短,如果直接进入park状态的话,程序在用户态和系统态之间的切换会影响锁性能。这个spin可以减少切换;
2. 进入spin状态如果成功获取到锁的话,需要出队列,出队列需要更新自己的头指针,如果位于队列前列,那么需要操作的时间会减少
但是,如果全部依靠这个机制,那么理所当然的,CAS更新队列头的操作会非常频繁。所以,引入了EntryList来减少争用:
这里写图片描述
假设Thread A是当前锁的Owner,接下来他要释放锁了,那么如果EntryList为null并且cxq不为null,就会从cxq末尾取出一个线程,放入EntryList(注意,EntryList为双向队列),并且标记EntryList其中一个线程为Successor(一般是头节点,这个EntryList的大小可能大于一,一般在notify时,后面会说到),这个Successor接下来会进入spin状态尝试获取锁(注意,在第一次自旋过去后,之后线程一直处于park状态)。如果获取成功,则成为owner,否则,回到EntryList中。
这种利用两个队列减少争用的算法,可以参考: Michael Scott’s “2Q” algorithm
接下来,进入我们的正题,wait方法。如果一个线程成为owner后,执行了wait方法,则会进入WaitSet:
Object.wait()底层实现

这里写图片描述

1void ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS) { 2 3 //检查线程合法性 4 Thread *const Self = THREAD; 5 assert(Self->is_Java_thread(), "Must be Java thread!"); 6 JavaThread *jt = (JavaThread *) THREAD; 7 DeferredInitialize(); 8 //检查当前线程是否拥有锁 9 CHECK_OWNER(); 10 EventJavaMonitorWait event; 11 // 检查中断位 12 if (interruptible && Thread::is_interrupted(Self, true) && !HAS_PENDING_EXCEPTION) { 13 if (JvmtiExport::should_post_monitor_waited()) { 14 JvmtiExport::post_monitor_waited(jt, this, false); 15 } 16 if (event.should_commit()) { 17 post_monitor_wait_event(&event, 0, millis, false); 18 } 19 TEVENT(Wait - ThrowIEX); 20 THROW(vmSymbols::java_lang_InterruptedException()); 21 return; 22 } 23 TEVENT(Wait); 24 assert(Self->_Stalled == 0, "invariant"); 25 Self->_Stalled = intptr_t(this); 26 jt->set_current_waiting_monitor(this); 27 28 //建立放入WaitSet中的这个线程的封装对象 29 ObjectWaiter node(Self); 30 node.TState = ObjectWaiter::TS_WAIT; 31 Self->_ParkEvent->reset(); 32 OrderAccess::fence(); 33 //用自旋方式获取操作waitset的lock,因为一般只有owner线程会操作这个waitset(无论是wait还是notify),所以竞争概率很小(除非响应interrupt事件才会有争用),采用spin方式效率高 34 Thread::SpinAcquire(&_WaitSetLock, "WaitSet - add"); 35 //添加到waitset 36 AddWaiter(&node); 37 //释放锁,代表现在线程已经进入了waitset,接下来要park了 38 Thread::SpinRelease(&_WaitSetLock); 39 40 if ((SyncFlags & 4) == 0) { 41 _Responsible = NULL; 42 } 43 intptr_t save = _recursions; // record the old recursion count 44 _waiters++; // increment the number of waiters 45 _recursions = 0; // set the recursion level to be 1 46 exit(true, Self); // exit the monitor 47 guarantee(_owner != Self, "invariant"); 48 49 // 确保没有unpark事件冲突影响本次park,方法就是主动post一次unpark 50 if (node._notified != 0 && _succ == Self) { 51 node._event->unpark(); 52 } 53 54 // 接下来就是park操作了 55 。。。。。。。。。 56 。。。。。。。。。 57}

当另一个owner线程调用notify时,根据Knob_MoveNotifyee这个值,决定将从waitset里面取出的一个线程放到哪里(cxq或者EntrySet)
Object.notify()底层实现

1void ObjectMonitor::notify(TRAPS) { 2 //检查当前线程是否拥有锁 3 CHECK_OWNER(); 4 if (_WaitSet == NULL) { 5 TEVENT(Empty - Notify); 6 return; 7 } 8 DTRACE_MONITOR_PROBE(notify, this, object(), THREAD); 9 //决定取出来的线程放在哪里 10 int Policy = Knob_MoveNotifyee; 11 //同样的,用自旋方式获取操作waitset的lock 12 Thread::SpinAcquire(&_WaitSetLock, "WaitSet - notify"); 13 ObjectWaiter *iterator = DequeueWaiter(); 14 if (iterator != NULL) { 15 TEVENT(Notify1 - Transfer); 16 guarantee(iterator->TState == ObjectWaiter::TS_WAIT, "invariant"); 17 guarantee(iterator->_notified == 0, "invariant"); 18 if (Policy != 4) { 19 iterator->TState = ObjectWaiter::TS_ENTER; 20 } 21 iterator->_notified = 1; 22 Thread *Self = THREAD; 23 iterator->_notifier_tid = Self->osthread()->thread_id(); 24 25 ObjectWaiter *List = _EntryList; 26 if (List != NULL) { 27 assert(List->_prev == NULL, "invariant"); 28 assert(List->TState == ObjectWaiter::TS_ENTER, "invariant"); 29 assert(List != iterator, "invariant"); 30 } 31 32 if (Policy == 0) { // prepend to EntryList 33 if (List == NULL) { 34 iterator->_next = iterator->_prev = NULL; 35 _EntryList = iterator; 36 } else { 37 List->_prev = iterator; 38 iterator->_next = List; 39 iterator->_prev = NULL; 40 _EntryList = iterator; 41 } 42 } else if (Policy == 1) { // append to EntryList 43 if (List == NULL) { 44 iterator->_next = iterator->_prev = NULL; 45 _EntryList = iterator; 46 } else { 47 // CONSIDER: finding the tail currently requires a linear-time walk of 48 // the EntryList. We can make tail access constant-time by converting to 49 // a CDLL instead of using our current DLL. 50 ObjectWaiter *Tail; 51 for (Tail = List; Tail->_next != NULL; Tail = Tail->_next); 52 assert(Tail != NULL && Tail->_next == NULL, "invariant"); 53 Tail->_next = iterator; 54 iterator->_prev = Tail; 55 iterator->_next = NULL; 56 } 57 } else if (Policy == 2) { // prepend to cxq 58 // prepend to cxq 59 if (List == NULL) { 60 iterator->_next = iterator->_prev = NULL; 61 _EntryList = iterator; 62 } else { 63 iterator->TState = ObjectWaiter::TS_CXQ; 64 for (;;) { 65 ObjectWaiter *Front = _cxq; 66 iterator->_next = Front; 67 if (Atomic::cmpxchg_ptr(iterator, &_cxq, Front) == Front) { 68 break; 69 } 70 } 71 } 72 } else if (Policy == 3) { // append to cxq 73 iterator->TState = ObjectWaiter::TS_CXQ; 74 for (;;) { 75 ObjectWaiter *Tail; 76 Tail = _cxq; 77 if (Tail == NULL) { 78 iterator->_next = NULL; 79 if (Atomic::cmpxchg_ptr(iterator, &_cxq, NULL) == NULL) { 80 break; 81 } 82 } else { 83 while (Tail->_next != NULL) Tail = Tail->_next; 84 Tail->_next = iterator; 85 iterator->_prev = Tail; 86 iterator->_next = NULL; 87 break; 88 } 89 } 90 } else { 91 ParkEvent *ev = iterator->_event; 92 iterator->TState = ObjectWaiter::TS_RUN; 93 OrderAccess::fence(); 94 ev->unpark(); 95 } 96 97 if (Policy < 4) { 98 iterator->wait_reenter_begin(this); 99 } 100 101 // _WaitSetLock protects the wait queue, not the EntryList. We could 102 // move the add-to-EntryList operation, above, outside the critical section 103 // protected by _WaitSetLock. In practice that's not useful. With the 104 // exception of wait() timeouts and interrupts the monitor owner 105 // is the only thread that grabs _WaitSetLock. There's almost no contention 106 // on _WaitSetLock so it's not profitable to reduce the length of the 107 // critical section. 108 } 109 //释放waitset的lock 110 Thread::SpinRelease(&_WaitSetLock); 111 112 if (iterator != NULL && ObjectMonitor::_sync_Notifications != NULL) { 113 ObjectMonitor::_sync_Notifications->inc(); 114 } 115}

对于NotifyAll就很好推测了,这里不再赘述;

(4)clone方法

默认的是浅拷贝,至于为什么,这里不赘述,我们直接看源码:
对于object的clone,我们需要考虑是否为数组,还有jvm环境等等,具体可以参考我的另一篇文章:垃圾收集分析(1)-Java对象结构(上)

1void LibraryCallKit::copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark) { 2 //首先做一些基本判断,例如对象不为空 3 assert(obj_size != NULL, ""); 4 Node* raw_obj = alloc_obj->in(1); 5 assert(alloc_obj->is_CheckCastPP() && raw_obj->is_Proj() && raw_obj->in(0)->is_Allocate(), ""); 6 7 AllocateNode* alloc = NULL; 8 if (ReduceBulkZeroing) { 9 // We will be completely responsible for initializing this object - 10 // mark Initialize node as complete. 11 alloc = AllocateNode::Ideal_allocation(alloc_obj, &_gvn); 12 // The object was just allocated - there should be no any stores! 13 guarantee(alloc != NULL && alloc->maybe_set_complete(&_gvn), ""); 14 // Mark as complete_with_arraycopy so that on AllocateNode 15 // expansion, we know this AllocateNode is initialized by an array 16 // copy and a StoreStore barrier exists after the array copy. 17 alloc->initialization()->set_complete_with_arraycopy(); 18 } 19 20 //分配初始指针 21 Node* src = obj; 22 Node* dest = alloc_obj; 23 Node* size = _gvn.transform(obj_size); 24 25 // 根据是否为数组决定对象头结构 26 int base_off = is_array ? arrayOopDesc::length_offset_in_bytes() : 27 instanceOopDesc::base_offset_in_bytes(); 28 // base_off: 29 // 8 - 32-bit VM 30 // 12 - 64-bit VM, compressed klass 31 // 16 - 64-bit VM, normal klass 32 if (base_off % BytesPerLong != 0) { 33 assert(UseCompressedClassPointers, ""); 34 if (is_array) { 35 // Exclude length to copy by 8 bytes words. 36 base_off += sizeof(int); 37 } else { 38 // Include klass to copy by 8 bytes words. 39 base_off = instanceOopDesc::klass_offset_in_bytes(); 40 } 41 assert(base_off % BytesPerLong == 0, "expect 8 bytes alignment"); 42 } 43 src = basic_plus_adr(src, base_off); 44 dest = basic_plus_adr(dest, base_off); 45 46 // Compute the length also, if needed: 47 Node* countx = size; 48 countx = _gvn.transform(new (C) SubXNode(countx, MakeConX(base_off))); 49 countx = _gvn.transform(new (C) URShiftXNode(countx, intcon(LogBytesPerLong) )); 50 51 const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM; 52 bool disjoint_bases = true; 53 generate_unchecked_arraycopy(raw_adr_type, T_LONG, disjoint_bases, 54 src, NULL, dest, NULL, countx, 55 /*dest_uninitialized*/true); 56 57 // If necessary, emit some card marks afterwards. (Non-arrays only.) 58 if (card_mark) { 59 assert(!is_array, ""); 60 // Put in store barrier for any and all oops we are sticking 61 // into this object. (We could avoid this if we could prove 62 // that the object type contains no oop fields at all.) 63 Node* no_particular_value = NULL; 64 Node* no_particular_field = NULL; 65 int raw_adr_idx = Compile::AliasIdxRaw; 66 post_barrier(control(), 67 memory(raw_adr_type), 68 alloc_obj, 69 no_particular_field, 70 raw_adr_idx, 71 no_particular_value, 72 T_OBJECT, 73 false); 74 } 75 76 // Do not let reads from the cloned object float above the arraycopy. 77 if (alloc != NULL) { 78 // Do not let stores that initialize this object be reordered with 79 // a subsequent store that would make this object accessible by 80 // other threads. 81 // Record what AllocateNode this StoreStore protects so that 82 // escape analysis can go from the MemBarStoreStoreNode to the 83 // AllocateNode and eliminate the MemBarStoreStoreNode if possible 84 // based on the escape status of the AllocateNode. 85 insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress)); 86 } else { 87 insert_mem_bar(Op_MemBarCPUOrder); 88 } 89}

(5) finalize()方法——对象的回收

当确定一个对象不会被其他方法再使用时,该对象就没有存在的意义了,就只能等待JVM的垃圾回收线程来回收了。垃圾回收是以占用一定内存资源为代价的。System.gc();就是启动垃圾回收线程的语句。当用户认为需要回收时,可以使用Runtime.getRuntime( ).gc( );或者System.gc();来回收内存。(System.gc();调用的就是Runtime类的gc( )方法)
当一个对象在回收前想要执行一些操作,就要覆写Object类中的finalize( )方法。

protected void finalize() throws Throwable { }

注意到抛出的是Throwable,说明除了常规的异常Exceprion外,还有可能是JVM错误。说明调用该方法不一定只会在程序中产生异常,还有可能产生JVM错误。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

00:Java简单了解

浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。