The java.lang.Object.wait() causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object's monitor (例如同步代码块). The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

public class TestWait
{

private List synchedList;

public TestWait() {
// create a new synchronized list to be used
synchedList = Collections.synchronizedList(new LinkedList());
}

// method used to remove an element from the list
public String removeElement() throws InterruptedException {
synchronized (synchedList) {

// while the list is empty, wait
while (synchedList.isEmpty()) {
System.out.println("List is empty...");
synchedList.wait();
System.out.println("Waiting...");
}
String element = (String) synchedList.remove(0);

return element;
}
}

// method to add an element in the list
public void addElement(String element) {
System.out.println("Opening...");
synchronized (synchedList) {

// add an element and notify all that an element exists
synchedList.add(element);
synchedList.add("test123");
System.out.println("New Element:'" + element + "'");

synchedList.notifyAll();
System.out.println("notifyAll called!");
}
System.out.println("Closing...");
}

public static void main(String[] args) {
final TestWait demo = new TestWait();

Runnable runA = new Runnable() {

public void run() {
try {
String item = demo.removeElement();
System.out.println("" + item);
} catch (InterruptedException ix) {
System.out.println("Interrupted Exception!");
} catch (Exception x) {
System.out.println("Exception thrown.");
}
}
};

Runnable runB = new Runnable() {

// run adds an element in the list and starts the loop
public void run() {
demo.addElement("Hello!");
}
};

try {
Thread threadA1 = new Thread(runA, "A");
threadA1.start();

Thread.sleep(500);

Thread threadA2 = new Thread(runA, "B");
threadA2.start();

Thread.sleep(500);

Thread threadB = new Thread(runB, "C");
threadB.start();

Thread.sleep(1000);

threadA1.interrupt();
threadA2.interrupt();
} catch (InterruptedException x) {
}
}
}

Object 的wait()方法的更多相关文章

  1. 理解JAVA - 面向对象(object) - 属性,方法

    理解JAVA - 面向对象(object) - 属性,方法 多态的体现:    向上造型,父类接收子类对象:向上造型:    从父类角度看不到子类独有的方法:面向对象,人类认知世界的方式:生活中每天都 ...

  2. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  3. Object类clone方法的自我理解

    网上搜帖: clone()是java.lang.Object类的protected方法,实现clone方法: 1)类自身需要实现Cloneable接口 2)需重写clone()方法,最好设置修饰符mo ...

  4. Cloneable接口和Object的clone()方法

    为什么要克隆 为什么要使用克隆,这其实反映的是一个很现实的问题,假如我们有一个对象: public class SimpleObject implements Cloneable { private ...

  5. JavaScript:Object.prototype.toString方法的原理

    在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...

  6. 方法object面试题分析:7JAVA中Object的clone方法详解-克隆-深克隆

    时间紧张,先记一笔,后续优化与完善.     每日一道理 翻开早已发黄的页张,试着寻找过去所留下的点点滴滴的足迹.多年前的好友似乎现在看来已变得陌生,匆忙之间,让这维持了多年的友谊变淡,找不出什么亲切 ...

  7. Object类的方法

    一 Object:类Object是类层次结构的跟类,每个类都使用Object作为超类,每个类都是直接或者间接的继承自Object类. Object类的方法: ①public int hashCode( ...

  8. javascript Object的新方法

    今天复习es6,又看到Object的一堆方法,与es5的表现又有不一致,耗费了一整天,整理一下: 前几天在司徒正美的书里又看到了es5 Object的字眼,为了向下兼容,大神们也是牛逼的整理出一系列i ...

  9. Object 及toString() 方法的重写

    Object: 是所有的类的父类  ,Object中所有的方法 , 子类都能使用  ,   接口不是Object子类. Person: /*将父类的equals方法 重写 * 不改变父类的源代码 eq ...

  10. Object中的方法以及对象相等的判定

    看图说话 Object有以下几个方法 getClass() final类型,主要是用来获得运行时的类型 hashCode() 返回该对象的哈希码值,方法是为了提高哈希表(例如 java.util.Ha ...

随机推荐

  1. Python语言的技术领域

    第一部分:各个领域应用的语言 大家看这个内容,其实你很明显发现,其实各个语言都有他的用处.我们可以说Python是应用最广的.但是暂时还是不能说它是全能的,因为他也有它的短板,但是对于一般的小公司和小 ...

  2. [刘阳Java]_Spring对Dao的支持_第10讲

    Spring框架优秀就是在于MVC开发的时候一旦需要对底层的数据库操作,它可以很好的支持JDBC技术,还有现在主流的ORM框架(Hibernate, MyBatis)技术. 重点先介绍Spring对J ...

  3. IO流 connect reset

    目录 出现场景 解决思路 出现场景 通过外部OBS下载10文件,然后通过工具将这10个文件打包成一个文件A.zip上传,最后将这个A.zip下载并解压,解压A.zip后发现文件数量不是10个. 解决思 ...

  4. 期望长度P1365,CF235B,P1654

    期望长度 定义 这里期望长度表示一段序列连续长度的期望.具体来说,对于一段序列,每个点都有一个概率连续和断开.求所有连续序列和的期望. 当然,对于以上期望长度的定义,我们只需要求出每个点存在的期望的和 ...

  5. Debian10 / Ubuntu 20.10 /Linux Mint 20安装Microsoft Edge浏览器Dev版(每周更新)

    安装方法如下: 打开终端命令,切换到 管理员身份,输入如下安装命令即可在Linux下使用 Microsoft Edge 浏览器了 ## Setup curl https://packages.micr ...

  6. Django orm 常用查询筛选总结

    本文主要列举一下django orm中的常用查询的筛选方法: 大于.大于等于 小于.小于等于 in like is null / is not null 不等于/不包含于 其他模糊查询 model: ...

  7. 谈谈 C++ STL 中的迭代器

    C++中的迭代器和指针 在前面的内容中我们简单讲过,STL主要是由三部分组成 容器(container),包括vector,list,set,map等 泛型算法(generic algorithm), ...

  8. Pdb— Python的调试器

    参考:Pdb- Python的调试器 pdb 模块定义了一个交互式源代码调试器,用于 Python 程序.它支持在源码行间设置(有条件的)断点和单步执行,检视堆栈帧,列出源码列表,以及在任何堆栈帧的上 ...

  9. 在vmware虚拟机下的Window2003服务器下安装IIS服务详细教程——超级详细(解决关于:800a0bb9的解决办法)

    总的来说,就是9步: 1.控制面板添加或者删除程序2.删除想要删的3.打开IIS配置4.开始共享5.导入源码6.配置权限7.网站属性.文档.应用程序配置8.web服务扩展9.访问网站 在安装好虚拟机的 ...

  10. jmeter正则表达式介绍

    分三个层次介绍: 1. jmeter正则表达式有什么作用? 2. 正则表达式在哪? 3. 正则表达式怎么用? 1. jmeter正则表达式有什么作用? 答:提取请求中返回的数据, 然后获取的数据放入其 ...