1. public final native Class<?> getClass();
  2.  
  3. public native int hashCode();
  4.  
  5. public boolean equals(Object obj) {
  6. return (this == obj);
  7. }
  8.  
  9. protected native Object clone() throws CloneNotSupportedException;
  10.  
  11. public String toString() {
  12. return getClass().getName() + "@" + Integer.toHexString(hashCode());
  13. }
  14.  
  15. public final native void notify();
  16.  
  17. public final native void notifyAll();
  18.  
  19. public final native void wait(long timeout) throws InterruptedException;
  20.  
  21. public final void wait() throws InterruptedException {
  22. wait(0);
  23. }
  24.  
  25. protected void finalize() throws Throwable { }

重点看看wait(),notify(),notifyAll()方法

首先是都是final native 修饰的。

monitor暂且译成监听器,相当于对象锁。

notify的注释:

  1. /**
  2. * Wakes up a single thread that is waiting on this object's
  3. * monitor. If any threads are waiting on this object, one of them
  4. * is chosen to be awakened. The choice is arbitrary and occurs at
  5. * the discretion of the implementation. A thread waits on an object's
  6. * monitor by calling one of the {@code wait} methods.

唤醒正在等待该对象的单个线程,如果有多个线程在等待获取某个对象的监听器,只唤醒其中一个。

  1. @throws IllegalMonitorStateException if the current thread is not
  2. * the owner of this object's monitor.

如果当前线程没拥有监听器就抛异常 IllegalMonitorStateException,这就是为什么notify,notifyAll必须在synchronized代码中执行。

notifyAll的注释:

  1. /**
  2. * Wakes up all threads that are waiting on this object's monitor. A
  3. * thread waits on an object's monitor by calling one of the
  4. * {@code wait} methods.

唤醒在该对象监视器上等待的所有线程,但是只有一个最终获得监听器。

  1. @throws IllegalMonitorStateException if the current thread is not
  2. * the owner of this object's monitor.

同上。

wait的注释:

  1. /**
  2. * Causes the current thread to wait until either another thread invokes the
  3. * {@link java.lang.Object#notify()} method or the
  4. * {@link java.lang.Object#notifyAll()} method for this object, or a
  5. * specified amount of time has elapsed.
  1. * <ul>
  2. * <li>Some other thread invokes the {@code notify} method for this
  3. * object and thread <var>T</var> happens to be arbitrarily chosen as
  4. * the thread to be awakened.
  5. * <li>Some other thread invokes the {@code notifyAll} method for this
  6. * object.
  7. * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
  8. * thread <var>T</var>.
  9. * <li>The specified amount of real time has elapsed, more or less. If
  10. * {@code timeout} is zero, however, then real time is not taken into
  11. * consideration and the thread simply waits until notified.
  12. * </ul>

导致当前线程等待,直到另一个线程调用notify(),notifyAll(),线程中断,或者超过指定的时间。

当调用wait(),实际上是调用的wait(0)表示无限等下去,也可以指定如wait(1000)表示等待一秒就继续执行。

wait()方法会放弃对象锁,sleep()不会。

  1. * the condition that should have caused the thread to be awakened, and
  2. * continuing to wait if the condition is not satisfied. In other words,
  3. * waits should always occur in loops, like this one:
  4. * <pre>
  5. * synchronized (obj) {
  6. * while (<condition does not hold>)
  7. * obj.wait(timeout);
  8. * ... // Perform action appropriate to condition
  9. * }
  10. * </pre>

建议wait(time)的方法放在while循环中。

  1. * @throws IllegalArgumentException if the value of timeout is
  2. * negative.
  3. * @throws IllegalMonitorStateException if the current thread is not
  4. * the owner of the object's monitor.
  5. * @throws InterruptedException if any thread interrupted the

当时间参数not in the range 0-999999,抛异常IllegalArgumentException

当线程未获得对象锁,抛异常IllegalMonitorStateException。这就是wait()要在synchronized中执行。

当线程被中断,抛异常InterruptedException

一个栗子:

  1. public class Test2 implements Runnable {
  2. static Object a = new Object();
  3. public static void main(String[] args) throws InterruptedException {
  4. Test2 t2 = new Test2();
  5. Thread thread = new Thread(t2);
  6. thread.start();
  7. //TimeUnit.MILLISECONDS.sleep(1000);
  8. synchronized (a) {
  9. System.out.println("notify start"+System.currentTimeMillis());
  10. a.notify();
  11. System.out.println("notify end"+System.currentTimeMillis());
  12. }
  13. }
  14.  
  15. @Override
  16. public void run() {
  17. synchronized (a) {
  18. try {
  19. System.out.println("wait start" + System.currentTimeMillis());
  20. a.wait();
  21. System.out.println("wait end" + System.currentTimeMillis());
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25.  
  26. }
  27. }
  28. }

当TimeUnit.MILLISECONDS.sleep(1000);注释掉时,控制台:

  1. notify start1517303479751
  2. notify end1517303479752
  3. wait start1517303479752

notify执行完了才执行wait,导致wait不能结束。意味着执行顺序不能依赖代码先后顺序。

JDK源码笔记--Object的更多相关文章

  1. 【java基础之jdk源码】Object

    最新在整体回归下java基础薄弱环节,以下为自己整理笔记,若有理解错误,请批评指正,谢谢. java.lang.Object为java所有类的基类,所以一般的类都可用重写或直接使用Object下方法, ...

  2. JDK源码阅读--Object

    在java.lang包下 Object类:是所有类的基类(父类) public final native Class<?> getClass(); 返回这个Object所代表的的运行时类 ...

  3. jdk源码阅读-Object类

    native 关键字 private static native void registerNatives(); static { registerNatives(); } public final ...

  4. JDK源码学习笔记——Object

    一.源码解析 public class Object { /** * 一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用 */ private static native void ...

  5. Java源码之Object

    本文出自:http://blog.csdn.net/dt235201314/article/details/78318399 一丶概述 JAVA中所有的类都继承自Object类,就从Object作为源 ...

  6. JDK源码学习笔记——LinkedHashMap

    HashMap有一个问题,就是迭代HashMap的顺序并不是HashMap放置的顺序,也就是无序. LinkedHashMap保证了元素迭代的顺序.该迭代顺序可以是插入顺序或者是访问顺序.通过维护一个 ...

  7. JDK源码阅读(一):Object源码分析

    最近经过某大佬的建议准备阅读一下JDK的源码来提升一下自己 所以开始写JDK源码分析的文章 阅读JDK版本为1.8 目录 Object结构图 构造器 equals 方法 getClass 方法 has ...

  8. jdk源码阅读笔记-HashSet

    通过阅读源码发现,HashSet底层的实现源码其实就是调用HashMap的方法实现的,所以如果你阅读过HashMap或对HashMap比较熟悉的话,那么阅读HashSet就很轻松,也很容易理解了.我之 ...

  9. 从JDK源码角度看Object

    Java的Object是所有其他类的父类,从继承的层次来看它就是最顶层根,所以它也是唯一一个没有父类的类.它包含了对象常用的一些方法,比如getClass.hashCode.equals.clone. ...

随机推荐

  1. 一键开启MacOS HiDPI

    完整文件下载:一键开启MacOS HiDPI 引言 作为一个黑苹果用户,追求黑果的体验是当然的,当各个硬件都驱动完善后,要做的就是细节的优化了,毕竟装上是拿来用的,可不能因为体验差苦了自己啊.机器毕竟 ...

  2. Map_占位符(嗨没想好怎么起名字)

    Map 是很好的集合,为我们带来了在其他语言(比如 Perl)中经常可见的好用的键/值对集合.JDK 以 HashMap 的形式为我们提供了方便的 Map 实现,它在内部使用哈希表实现了对键的对应值的 ...

  3. dubbo基础

    一.什么是dubbo,有什么用 dubbo是阿里巴巴开源的一个RPC框架,用于多个应用相互通信.使用dubbo需要安装一zookepper 二.dubbo的基本使用 1.构建一个maven的多模块项目 ...

  4. springboot整合mybatis出现的一些问题

    springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...

  5. JVM jinfo命令(Java Configuration Info) 用法小结

    简介 jinfo是jdk自带的命令,可以用来查看正在运行的Java应用程序的扩展参数,甚至支持在运行时,修改部分参数. 通常会先使用jps查看java进程的id,然后使用jinfo查看指定pid的jv ...

  6. JAVA-8大基本类型与包装类的例子(基础必备)

    package com.net.xinfang.reflect; /*** * 8种基本类型(byte,int,short,long,float,double,char,boolean) * 布尔型只 ...

  7. Dash VS Underscore

    Dash Dashes are recommended by Google over underscores (source). Dashes are more familiar to the end ...

  8. Java Service Wrapper 使用

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sinat_26279177/article/details/70807173 1       简介 ...

  9. Spark源码解析 - Spark-shell浅析

    1.准备工作 1.1 安装spark,并配置spark-env.sh 使用spark-shell前需要安装spark,详情可以参考http://www.cnblogs.com/swordfall/p/ ...

  10. 服务器部署全程记录(centos6.5)

    1.安装nginx 上传安装包:put E:\yz_index\installPackage\nginx-1.14.0.tar.gz 解压:tar zxvf nginx-1.14.0.tar.gz 切 ...