Object.wait 中JDK提供的doc文档

Causes the 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.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
} 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.

  

一个对象的monitor只能被一个线程占用,wait()方法会释放这个对象的锁, 既然要释放 就先要取得这个锁, 取得对象锁的方式只有synchronized()。释放锁之后, 线程进入BLOCK状态

doc文档中说明调用wait的时机是因为运行条件condition不满足,

比如生产者没有往队列中放东西,队列已经空了。队列不为空的时候,再调用obj.notify()。此时condation==队列,obj是针对这个队列的线程间共享变量,也可以是队列本身(最好不是,会阻塞生产者的线程)?

object.wait为什么要和synchronized一块使用的更多相关文章

  1. 线程同步(使用了synchronized)和线程通讯(使用了wait,notify)

    线程同步 什么是线程同步? 当使用多个线程来访问同一个数据时,非常容易出现线程安全问题(比如多个线程都在操作同一数据导致数据不一致),所以我们用同步机制来解决这些问题. 实现同步机制有两个方法:1.同 ...

  2. Synchronized的使用和注意事项

    synchronized: 1.取得的锁都是对象锁,而不是把一段代码或方法(函数)当作锁: 2.多个线程访问的必须是同一个对象. 3.当一个线程执行的代码出现异常时,其所持有的锁会自动释放 4.A线程 ...

  3. synchronized实现原理及其优化-(自旋锁,偏向锁,轻量锁,重量锁)

    1.synchronized概述: synchronized修饰的方法或代码块相当于并发中的临界区,即在同一时刻jvm只允许一个线程进入执行.synchronized是通过锁机制实现同一时刻只允许一个 ...

  4. 基础篇:Object对象

    目录 1 Object的内存结构和指针压缩了解一下 2 Object的几种基本方法 3 == . equals.Comparable.compareTo.Comparator.compara 四种比较 ...

  5. 【多线程与高并发原理篇:4_深入理解synchronized】

    1. 前言 越是简单的东西,在深入了解后发现越复杂.想起了曾在初中阶段,语文老师给我们解说<论语>的道理,顺便给我们提了一句,说老子的无为思想比较消极,学生时代不要太关注.现在有了一定的生 ...

  6. synchronized原理剖析

    synchronized原理剖析 并发编程存在什么问题? 1️⃣ 可见性 可见性:是指当一个线程对共享变量进行了修改,那么另外的线程可以立即看到修改后的最新值. 案例演示:一个线程A根据 boolea ...

  7. Java多线程4:synchronized锁机制

    脏读 一个常见的概念.在多线程中,难免会出现在多个线程中对同一个对象的实例变量进行并发访问的情况,如果不做正确的同步处理,那么产生的后果就是"脏读",也就是取到的数据其实是被更改过 ...

  8. Java Synchronized Blocks

    From http://tutorials.jenkov.com/java-concurrency/synchronized.html By Jakob Jenkov   A Java synchro ...

  9. 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法(转)

    对象的synchronized方法不能进入了,但它的其他非synchronized方法还是可以访问的 对每一个class只有一个thread可以执行synchronized static method ...

随机推荐

  1. Oracle 12c 多租户 CDB 与 PDB 级别 expdb 与 impdb(表、用户、全库)

    Oracle 数据库 12 c 多租户下,如何在容器数据库 (CDB) 和可插拔数据库 (PDB) 中使用 expdb 与 impdp (数据泵) 呢? 我们一起探讨下PDB 下进行表级,用户级别,全 ...

  2. javascript:delete 删除对象的属性

    delete 运算符删除对以前定义的对象属性或方法的引用. 不可以删除的如下: 1通过var定义的变量 var a=1;delete a//false 2 声明后的函数 function a(){}; ...

  3. docker-ce安装与搭建私有仓库

    https://www.cnblogs.com/sszhou/p/7389144.html 系统环境centos7 ###docker-ce安装###1.卸载老版本,较老版本的Docker被称为doc ...

  4. python中string和bool的转换

    python中字符串"True" 和 "False"转为bool类型时, 不能通过bool(xx)强转. 注意是因为在python中,除了&apos;& ...

  5. shell ## %% 使用说明

    path='apps/home/usr/app/test.txt' a=${path##*/} b=${path#*/} c=${path%%/*} d=${path%/*}············· ...

  6. Go中使用动态库C/C++库

    转自:http://studygolang.com/articles/1441 最近需要做一些在go中使用动态C++库的工作,经常碰到找不到动态库路径这种情况,所以就花点时间,专门做一下实验来了解Go ...

  7. 关于导入excel报错的处理(xls,xlsx)

    关于导入excel报错的处理(xls,xlsx) 最近在做一个将excel导入到dataGriview中的小功能在做的过程中遇到以下问题: 链接excel的链接串是这样写的 string strCon ...

  8. Hive 启动 Diagnostic Messages for this Task: java.lang.Throwable: Child Error

    Diagnostic Messages for this Task: java.lang.Throwable: Child Error at org.apache.hadoop.mapred.Task ...

  9. div中嵌套的多个div使用了浮动后居中的办法

    今天做网页的时候遇到了标题中的问题,网上查到了解决办法,记录一下一放以后忘记 <div class="wai"> <div style="float:l ...

  10. js验证文本框数字

    输入框 <input name="title" type="text" oninput="onlyNum(this,'')" titl ...