JDK源码--Object

2019-07-29  本文已影响0人  TomyZhang

一、使用

public final native Class<?> getClass()

public String toString()

public native int hashCode()

public boolean equals(Object obj)

//TestObject
public class TestObject {
    private static final String TAG = "TestObject";
    private Object object = new Object();
    private Object object2 = new Object();

    public void doSomething() {
        Log.d(TAG, "zwm, doSomething");
        Log.d(TAG, "zwm, object.getClass: " + object.getClass());
        Log.d(TAG, "zwm, object.toString: " + object.toString());
        Log.d(TAG, "zwm, object.hashCode: " + object.hashCode());
        Log.d(TAG, "zwm, object.equals(object2): " + object.equals(object));
        Log.d(TAG, "zwm, object.equals(object2): " + object.equals(object2));
    }
}

//测试代码
private void testMethod() {
    Log.d(TAG, "zwm, testMethod");
    TestObject testObject = new TestObject();
    testObject.doSomething();
}

//输出log
2019-07-29 13:18:08.441 zwm, testMethod
2019-07-29 13:18:08.442 zwm, doSomething
2019-07-29 13:18:08.442 zwm, object.getClass: class java.lang.Object
2019-07-29 13:18:08.442 zwm, object.toString: java.lang.Object@3acb22
2019-07-29 13:18:08.442 zwm, object.hashCode: 3853090
2019-07-29 13:18:08.442 zwm, object.equals(object): true
2019-07-29 13:18:08.442 zwm, object.equals(object2): false

public final void wait() throws InterruptedException

public final native void notify()

//TestObject
public class TestObject {
    private static final String TAG = "TestObject";
    private Object object = new Object();

    public void testWait() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "zwm, testWait run, thread: " + Thread.currentThread().getName());
                synchronized (object) {
                    try {
                        Log.d(TAG, "zwm, before wait, thread: " + Thread.currentThread().getName());
                        object.wait();
                        Log.d(TAG, "zwm, after wait, thread: " + Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        Log.d(TAG, "zwm, InterruptedException, thread: " + Thread.currentThread().getName());
                    }
                }
                Log.d(TAG, "zwm, testWait run end, thread: " + Thread.currentThread().getName());
            }
        });
        thread.start();
    }

    public void testNotify() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "zwm, testNotify run, thread: " + Thread.currentThread().getName());
                synchronized (object) {
                    Log.d(TAG, "zwm, notify, thread: " + Thread.currentThread().getName());
                    object.notify(); //只唤醒一个线程
                }
                Log.d(TAG, "zwm, testNotify run end, thread: " + Thread.currentThread().getName());
            }
        });
        thread.start();
    }
}

//测试代码
private void testMethod() {
    Log.d(TAG, "zwm, testMethod");
    final TestObject testObject = new TestObject();
    testObject.testWait();
    testObject.testWait();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            testObject.testNotify();
        }
    }, 2000);
}

//输出log
2019-07-29 13:47:42.244 zwm, testMethod
2019-07-29 13:47:42.247 zwm, testWait run, thread: Thread-10
2019-07-29 13:47:42.247 zwm, before wait, thread: Thread-10
2019-07-29 13:47:42.251 zwm, testWait run, thread: Thread-11
2019-07-29 13:47:42.252 zwm, before wait, thread: Thread-11
2019-07-29 13:47:44.251 zwm, testNotify run, thread: Thread-12
2019-07-29 13:47:44.251 zwm, notify, thread: Thread-12
2019-07-29 13:47:44.251 zwm, testNotify run end, thread: Thread-12
2019-07-29 13:47:44.252 zwm, after wait, thread: Thread-10
2019-07-29 13:47:44.252 zwm, testWait run end, thread: Thread-10

public final void wait() throws InterruptedException

public final native void notifyAll()

//TestObject
public class TestObject {
    private static final String TAG = "TestObject";
    private Object object = new Object();

    public void testWait() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "zwm, testWait run, thread: " + Thread.currentThread().getName());
                synchronized (object) {
                    try {
                        Log.d(TAG, "zwm, before wait, thread: " + Thread.currentThread().getName());
                        object.wait();
                        Log.d(TAG, "zwm, after wait, thread: " + Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        Log.d(TAG, "zwm, InterruptedException, thread: " + Thread.currentThread().getName());
                    }
                }
                Log.d(TAG, "zwm, testWait run end, thread: " + Thread.currentThread().getName());
            }
        });
        thread.start();
    }

    public void testNotify() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "zwm, testNotify run, thread: " + Thread.currentThread().getName());
                synchronized (object) {
                    Log.d(TAG, "zwm, notifyAll, thread: " + Thread.currentThread().getName());
                    object.notifyAll(); //唤醒所有线程
                }
                Log.d(TAG, "zwm, testNotify run end, thread: " + Thread.currentThread().getName());
            }
        });
        thread.start();
    }
}

//测试代码
private void testMethod() {
    Log.d(TAG, "zwm, testMethod");
    final TestObject testObject = new TestObject();
    testObject.testWait();
    testObject.testWait();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            testObject.testNotify();
        }
    }, 2000);
}

//输出log
2019-07-29 13:49:05.258 zwm, testMethod
2019-07-29 13:49:05.265 zwm, testWait run, thread: Thread-10
2019-07-29 13:49:05.265 zwm, before wait, thread: Thread-10
2019-07-29 13:49:05.270 zwm, testWait run, thread: Thread-11
2019-07-29 13:49:05.270 zwm, before wait, thread: Thread-11
2019-07-29 13:49:07.268 zwm, testNotify run, thread: Thread-12
2019-07-29 13:49:07.269 zwm, notifyAll, thread: Thread-12
2019-07-29 13:49:07.269 zwm, testNotify run end, thread: Thread-12
2019-07-29 13:49:07.269 zwm, after wait, thread: Thread-10
2019-07-29 13:49:07.269 zwm, testWait run end, thread: Thread-10
2019-07-29 13:49:07.270 zwm, after wait, thread: Thread-11
2019-07-29 13:49:07.270 zwm, testWait run end, thread: Thread-11

public final native void wait(long timeout) throws InterruptedException

public final void wait(long timeout, int nanos) throws InterruptedException

//TestObject
public class TestObject {
    private static final String TAG = "TestObject";
    private Object object = new Object();

    public void testWait() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "zwm, testWait run, thread: " + Thread.currentThread().getName());
                synchronized (object) {
                    try {
                        Log.d(TAG, "zwm, before wait, thread: " + Thread.currentThread().getName());
                        object.wait(3000); //long timeout
                        //object.wait(3000, 999999); //long timeout, int nanos
                        Log.d(TAG, "zwm, after wait, thread: " + Thread.currentThread().getName());
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        Log.d(TAG, "zwm, InterruptedException, thread: " + Thread.currentThread().getName());
                    }
                }
                Log.d(TAG, "zwm, testWait run end, thread: " + Thread.currentThread().getName());
            }
        });
        thread.start();
    }
}

//测试代码
private void testMethod() {
    Log.d(TAG, "zwm, testMethod");
    final TestObject testObject = new TestObject();
    testObject.testWait();
    testObject.testWait();
}

//输出log
2019-07-29 13:52:05.957 zwm, testMethod
2019-07-29 13:52:05.964 zwm, testWait run, thread: Thread-11
2019-07-29 13:52:05.964 zwm, testWait run, thread: Thread-10
2019-07-29 13:52:05.964 zwm, before wait, thread: Thread-11
2019-07-29 13:52:05.965 zwm, before wait, thread: Thread-10
2019-07-29 13:52:08.965 zwm, after wait, thread: Thread-11
2019-07-29 13:52:10.966 zwm, testWait run end, thread: Thread-11
2019-07-29 13:52:10.967 zwm, after wait, thread: Thread-10
2019-07-29 13:52:12.968 zwm, testWait run end, thread: Thread-10

protected Object clone() throws CloneNotSupportedException

protected void finalize() throws Throwable

//TestObject
public class TestObject implements Cloneable{
    private static final String TAG = "TestObject";
    public Object object = new Object();

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Log.d(TAG, "zwm, clone, this: " + this);
        return super.clone();
    }

    @Override
    protected void finalize() throws Throwable {
        Log.d(TAG, "zwm, finalize, this: " + this);
        super.finalize();
    }
}

//测试代码
private void testMethod() {
    Log.d(TAG, "zwm, testMethod");
    TestObject testObject = new TestObject();
    TestObject cloneObject = null;
    try {
        cloneObject = (TestObject)testObject.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    Log.d(TAG, "zwm, testObject: " + testObject);
    Log.d(TAG, "zwm, cloneObject: " + cloneObject);
    Log.d(TAG, "zwm, testObject.object: " + testObject.object);
    Log.d(TAG, "zwm, cloneObject.object: " + cloneObject.object);
    Log.d(TAG, "zwm, cloneObject.equals(object): " + cloneObject.object);
    Log.d(TAG, "zwm, cloneObject==object: " + (cloneObject==object));
}

//输出log
2019-07-29 15:00:01.739 zwm, testMethod
2019-07-29 15:00:01.739 zwm, clone, this: com.tomorrow.testnetworkcache.TestObject@3acb22
2019-07-29 15:00:01.740 zwm, testObject: com.tomorrow.testnetworkcache.TestObject@3acb22
2019-07-29 15:00:01.740 zwm, cloneObject: com.tomorrow.testnetworkcache.TestObject@13909b3
2019-07-29 15:00:01.740 zwm, testObject.object: java.lang.Object@9afdb70
2019-07-29 15:00:01.740 zwm, cloneObject.object: java.lang.Object@9afdb70
2019-07-29 15:00:01.740 zwm, cloneObject.equals(object): false
2019-07-29 15:00:01.740 zwm, cloneObject==object: false
2019-07-29 15:00:03.538 zwm, finalize, this: com.tomorrow.testnetworkcache.TestObject@3acb22
2019-07-29 15:00:03.538 zwm, finalize, this: com.tomorrow.testnetworkcache.TestObject@13909b3

二、原理

private static native void registerNatives()

static {
    registerNatives();
}

在Object类的静态代码块中调用registerNatives()方法,会在Object类加载的时候执行,目的是将Java方法映射到本地方法。

public final native Class<?> getClass()

本地方法,不能被重写。能够获取一个类的定义信息,然后使用反射去访问类的全部信息(包括成员变量及成员方法)。

public String toString()

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

获取class名称加上@再加上十六进制的hashCode。自定义类可以重写toString()方法实现自己的输出格式。

public native int hashCode()

本地方法,返回一个int类型的数值。hashCode()方法主要配合基于散列的集合一起工作,例如HashMap、HashSet等。

public boolean equals(Object obj)

public boolean equals(Object obj) {
    return (this == obj);
}

比较两个对象是否相等,比较的是对象引用,即"地址值"。自定义类可以重写equals(Object obj)方法实现自己的比较逻辑。

public final void wait() throws InterruptedException

public final native void wait(long timeout) throws InterruptedException

public final void wait(long timeout, int nanos) throws InterruptedException

public final native void notify()

public final native void notifyAll()

public final void wait() throws InterruptedException {
    wait(0); //调用wait(long timeout)
}

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 >= 500000 || (nanos != 0 && timeout == 0)) {
        timeout++;
    }

    wait(timeout); //调用wait(long timeout)
}

wait方法的作用是使线程进入休眠状态并释放对象锁。

notify()方法的作用是随机唤醒一个线程。notifyAll()方法的作用是唤醒所有的线程。

protected Object clone() throws CloneNotSupportedException

主要实现对象的克隆功能,即根据该对象生成一个新对象,如果成员变量是基本类型则会克隆值,如果成员变量是对象类型则会克隆对象的地址。自定义类要实现克隆功能需要实现Cloneable接口,并重写clone()方法。可以看到clone()方法会抛出一个CloneNotSupportedException异常,如果类没有实现Cloneable接口而该类对象调用了clone()方法,系统就会抛出这个异常。

protected void finalize() throws Throwable

对象在被虚拟机垃圾回收时可能会执行该方法。该方法具有不确定性,不建议使用。

三、主题

System.out.println(Object x)会调用Object的toString()方法

//System
public final static PrintStream out;

//PrintStream
public void println(Object x) {
    String s = String.valueOf(x);
    synchronized (this) {
        print(s);
        newLine();
    }
}

//String
public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}
上一篇下一篇

猜你喜欢

热点阅读