Java源码阅读--任重而道远(lang)
registerNatives:注冊(cè)本地方法,請(qǐng)看超鏈接
?private static native void registerNatives();
??? static {
??????? registerNatives();
??? }
該方法還在System等其他地方有使用
blog.csdn.net/chenyi8888/article/details/7070367
?getClass();
hashCode()
the hashCode method defined by
???? * class {@code Object} does return distinct integers for distinct
???? * objects.
為每個(gè)不同的對(duì)象返回一個(gè)不同的哈希值??hash可以相同嗎?????
equals
??? public boolean equals(Object obj) {
??????? return (this == obj);
??? }
clone
?? protected native Object clone() throws CloneNotSupportedException;
鏈接--深克隆,淺克隆
??? public String toString() {
??????? return getClass().getName() + "@" + Integer.toHexString(hashCode());
??? }
源代碼
public final void wait() throws InterruptedException {wait(0);}? ? ? public final void wait(long timeout, int nanos) throws InterruptedException {if (timeout < 0) {throw new IllegalArgumentException("timeout value is negative");}if (nanos < 0 || nanos > 999999) {throw new IllegalArgumentException("nanosecond timeout value out of range");}if (nanos > 0) {timeout++;}wait(timeout);} public final native void wait(long timeout) throws InterruptedException;? ? public final native void notify();? public final native void notifyAll();? ? public String toString() {return getClass().getName() + "@" + Integer.toHexString(hashCode());}? ? public boolean equals(Object obj) {return (this == obj);} public native int hashCode(); ?public final native Class<?> getClass();? ? //絕大多數(shù)情況下這里都已經(jīng)被改寫 public boolean equals(Object obj) {return (this == obj);} 改寫equals的例子(從中可以看出==和equals的區(qū)別,==表示堆內(nèi)存比較,equals表示根據(jù)重寫邏輯判斷是一致的,這就是為什么對(duì)象比較需要使用equals的原因) package springstudy;public class ObjectStudy {public static void main(final String[] args) {// 在沒有重寫equals的情況下,equals和==都表示存放在堆中的內(nèi)存對(duì)象final Object a = new Object();final Object a1 = new Object();System.out.println(a.equals(a1));// 沒有重寫,兩者都表示堆中數(shù)據(jù),相同為trueSystem.out.println(a == a1);// truefinal Object c = a;// 傳引用,兩者仍然表示堆中同一個(gè)對(duì)象System.out.println(c.equals(a));// trueSystem.out.println(c == a);// true//提示我們需要判斷某個(gè)對(duì)象是否是傳引用的時(shí)候,直接a==b,如果為true則為傳引用,否則不是final B b1 = new B();final B b2 = new B();System.out.println(b1.equals(b2));// 根據(jù)equals重寫,trueSystem.out.println(b1 == b2);// false,不是堆中同一個(gè)對(duì)象} }// 看一個(gè)例子(為什么equals和“==”不同,在最開始的實(shí)現(xiàn)中他們其實(shí)是相同的) class B {private String aString;private String bString;//重寫equals@Overridepublic boolean equals(final Object obj) {if (this == obj) {//當(dāng)內(nèi)存中相同return true;}//其他條件判斷if (obj == null) {return false;}if (this.getClass() != obj.getClass()) {return false;}final B other = (B) obj;if (this.aString == null) {if (other.aString != null) {return false;}}else if (!this.aString.equals(other.aString)) {return false;}if (this.bString == null) {if (other.bString != null) {return false;}}else if (!this.bString.equals(other.bString)) {return false;}return true;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((this.aString == null) ? 0 : this.aString.hashCode());result = prime * result + ((this.bString == null) ? 0 : this.bString.hashCode());return result;}}??? 一個(gè)生產(chǎn)者消費(fèi)者模型,其實(shí)代碼注釋里面就有提到進(jìn)行如下實(shí)現(xiàn) package com.wang.study;import java.util.Queue; import java.util.Random;import com.google.common.collect.Queues;public class ObjectStudy {Random random = new Random();public static void main(String[] args) {Queue<Integer> queue = Queues.newConcurrentLinkedQueue();int maxSize = 10;new ObjectStudy(queue, maxSize);}public ObjectStudy(final Queue<Integer> queue, final int maxSize) {Producer producer = new Producer(queue, maxSize);Consumer consumer = new Consumer(queue);Thread producerThread = new Thread(producer);Thread consumerThread = new Thread(consumer);producerThread.start();consumerThread.start();}private class Consumer implements Runnable {private Queue<Integer> queue;public Consumer(final Queue<Integer> queue) {this.queue = queue;}public void run() {while (true) {synchronized (queue) {if (queue.size() == 0) {try {queue.wait();} catch (InterruptedException e) {e.printStackTrace();}}Integer value = queue.poll();System.out.println(getClass().getName() + "------" + value);queue.notify();try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}}}}}private class Producer implements Runnable {private Queue<Integer> queue;private int maxSize;private int hello = 0;public Producer(final Queue<Integer> queue, final int maxSize) {this.queue = queue;this.maxSize = maxSize;}public void run() {while (true) {synchronized (queue) {if (queue.size() == maxSize) {try {queue.wait();} catch (InterruptedException e) {e.printStackTrace();}}hello++;queue.add(hello);System.out.println(getClass().getName() + "------" + hello);queue.notify();try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}}}}} }總結(jié)
以上是生活随笔為你收集整理的Java源码阅读--任重而道远(lang)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言编程学多久,丰城c语言编程学习,丰
- 下一篇: Wonderware-InTouch历史