Java源码阅读--任重而道远(lang)
registerNatives:注冊本地方法,請看超鏈接
?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.
為每個不同的對象返回一個不同的哈希值??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();? ? //絕大多數情況下這里都已經被改寫 public boolean equals(Object obj) {return (this == obj);} 改寫equals的例子(從中可以看出==和equals的區別,==表示堆內存比較,equals表示根據重寫邏輯判斷是一致的,這就是為什么對象比較需要使用equals的原因) package springstudy;public class ObjectStudy {public static void main(final String[] args) {// 在沒有重寫equals的情況下,equals和==都表示存放在堆中的內存對象final Object a = new Object();final Object a1 = new Object();System.out.println(a.equals(a1));// 沒有重寫,兩者都表示堆中數據,相同為trueSystem.out.println(a == a1);// truefinal Object c = a;// 傳引用,兩者仍然表示堆中同一個對象System.out.println(c.equals(a));// trueSystem.out.println(c == a);// true//提示我們需要判斷某個對象是否是傳引用的時候,直接a==b,如果為true則為傳引用,否則不是final B b1 = new B();final B b2 = new B();System.out.println(b1.equals(b2));// 根據equals重寫,trueSystem.out.println(b1 == b2);// false,不是堆中同一個對象} }// 看一個例子(為什么equals和“==”不同,在最開始的實現中他們其實是相同的) class B {private String aString;private String bString;//重寫equals@Overridepublic boolean equals(final Object obj) {if (this == obj) {//當內存中相同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;}}??? 一個生產者消費者模型,其實代碼注釋里面就有提到進行如下實現 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();}}}}} }總結
以上是生活随笔為你收集整理的Java源码阅读--任重而道远(lang)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言编程学多久,丰城c语言编程学习,丰
- 下一篇: java 获取gif帧数_Java图片处