public final native Class<?> getClass();

public native int hashCode();

public boolean equals(Object obj) {
return (this == obj);
} protected native Object clone() throws CloneNotSupportedException; public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
} public final native void notify(); public final native void notifyAll(); public final native void wait(long timeout) throws InterruptedException; public final void wait() throws InterruptedException {
wait(0);
} protected void finalize() throws Throwable { }

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

首先是都是final native 修饰的。

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

notify的注释:

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

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

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

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

notifyAll的注释:

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

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

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

同上。

wait的注释:

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

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

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

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

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

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

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

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

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

当线程被中断,抛异常InterruptedException

一个栗子:

public class Test2 implements Runnable {
static Object a = new Object();
public static void main(String[] args) throws InterruptedException {
Test2 t2 = new Test2();
Thread thread = new Thread(t2);
thread.start();
//TimeUnit.MILLISECONDS.sleep(1000);
synchronized (a) {
System.out.println("notify start"+System.currentTimeMillis());
a.notify();
System.out.println("notify end"+System.currentTimeMillis());
}
} @Override
public void run() {
synchronized (a) {
try {
System.out.println("wait start" + System.currentTimeMillis());
a.wait();
System.out.println("wait end" + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}

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

notify start1517303479751
notify end1517303479752
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. javascript实现全选,全不选,反选

    利用input的checked属性来实现:checked值为true,表示被选择,否则反之. 效果如图: js代码: <script> var butt1 = document.getEl ...

  2. Flink HA 搭建坑

    目前网上能找到的做HA的教程基本都无法真正做到多机高可用,包括官方文档,经过很久的折腾,终于做到了多机高可用,希望其它人不再被坑. 集群模式安装 前提条件: 机器已经安装好Java环境 jobMana ...

  3. 网络编程基础【day09】:通过socket实现简单ssh客户端(三)

    本节内容 1.概述 2.socket发送中文 3.重复发送和多次接收 4.模拟ssh客户端 一.概述 本篇博客讲一下,如果socket客户端断了,另外的客户端怎么接入服务端,还有模拟ssh的链接等. ...

  4. WEB网站类型系统中使用的OFFICE控件

    WEB下使用的OFFICE控件介绍,另提供一个原创破解首先来个名词解释,Office网络文档控件,就是在网页中编辑office文档的控件(前提是browser已经安装OFFICE).最近一个项目需要用 ...

  5. 原生JavaScript运动功能系列(四):多物体多值链式运动

    原生JavaScript运动功能系列(一):运动功能剖析与匀速运动实现 原生JavaScript运动功能系列(二):缓冲运动 原生JavaScript运动功能系列(三):多物体多值运动 多物体多值链式 ...

  6. bzoj千题计划320:bzoj4939: [Ynoi2016]掉进兔子洞(莫队 + bitset)

    https://www.lydsy.com/JudgeOnline/problem.php?id=4939 ans= r1-l1+1 + r2-l2+1 +r3-l3+1 - ∑ min(cnt1[i ...

  7. Spark源码剖析 - 计算引擎

    本章导读 RDD作为Spark对各种数据计算模型的统一抽象,被用于迭代计算过程以及任务输出结果的缓存读写.在所有MapReduce框架中,shuffle是连接map任务和reduce任务的桥梁.map ...

  8. while应用和函数学习

    # ******************************练习****************************# 在控制台中获取两个整数,作为循环开始和结束的点'''a = int(in ...

  9. 15个新鲜出炉的 Photoshop 文本效果教程

    文本效果可能是 Photoshop 图形设计中最常用和最通用的技术之一.最重要的是你可以使用任何效果,风格或纹理来产生有趣的排版,越多人尝试过它并制作了一些精彩的教程.所以这篇文章旨在为您提供全面的 ...

  10. Andrew NG 机器学习编程作业6 Octave

    问题描述:使用SVM(支持向量机 )实现一个垃圾邮件分类器. 在开始之前,先简单介绍一下SVM ①从逻辑回归的 cost function 到SVM 的 cost function 逻辑回归的假设函数 ...