Object的wait()

wait()搭配notify(),nofityAll()使用。

线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notify()后,线程重新开始运行。

final static Object async = new Object();

    public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (async) {
System.out.println("thread a get async");
try {
System.out.println("thread a start wait");
async.wait();
System.out.println("thread a end wait");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start(); new Thread(new Runnable() {
@Override
public void run() {
synchronized (async){
System.out.println("thread b get async");
System.out.println("thread b notify async");
async.notify();
}
}
}).start();
}

输出:

thread a get async
thread a start wait
thread b get async
thread b notify async
thread a end wait

Thread.sleep()

线程获取到对象锁之后,sleep时不会释放对象锁,其他线程也不能获取到对象锁。直到线程sleep结束,其他线程才能获取到对象锁。

final static Object async = new Object();

    public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (async) {
System.out.println("thread a get async");
try {
System.out.println("thread a start sleep");
Thread.sleep(1000);
System.out.println("thread a end sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start(); new Thread(new Runnable() {
@Override
public void run() {
synchronized (async) {
System.out.println("thread b get async");
System.out.println("thread b notify async");
async.notify();
}
}
}).start();
}

输出:

thread a get async
thread a start sleep
thread a end sleep
thread b get async
thread b notify async

Object的wait和Thread的sleep的更多相关文章

  1. notification:object not locked by thread before notify()

    今天写notification练习时,误将NotificationManager.notify(0, notification);写成notification.notify(); 代码如下 publi ...

  2. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

  3. 'module' object has no attribute 'Thread'解决方法及模块加载顺序

    源码片段: class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Threa ...

  4. Object not locked by thread before notify() in onPostExecute

    Ask Question Asked 5 years, 4 months ago Active 3 years, 9 months ago Viewed 56k time 41 2 I try to ...

  5. 为什么等待和通知是在 Object 类而不是 Thread 中声明的?

    一个棘手的 Java 问题,如果 Java编程语言不是你设计的,你怎么能回答这个问题呢.Java编程的常识和深入了解有助于回答这种棘手的 Java 核心方面的面试问题.为什么 wait,notify ...

  6. java之Thread.sleep(long)与object.wait()/object.wait(long)的区别及相关概念梳理(good)

    一.Thread.sleep(long)与object.wait()/object.wait(long)的区别sleep(long)与wait()/wait(long)行为上有些类似,主要区别如下:1 ...

  7. 多线程爬坑之路-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

  8. Android笔记——Handler Runnable与Thread的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

  9. java 多线程 Synchronized方法和方法块 synchronized(this)和synchronized(object)的理解

    synchronized 关键字,它包括两种用法:synchronized 方法和 synchronized 块. 1. synchronized 方法:通过在方法声明中加入 synchronized ...

随机推荐

  1. servlet 中文编码设置

    Tomcat服务器默认采用的ISO8859-1编码 产生的原因: 不同数据来源的编(解)码格式不同: 数据来源 默认编码格式 浏览器页面 GBK(可在浏览器页面右键切换) request(get) I ...

  2. 原生js通过最外层id获取下面指定的子元素

    需求:在vue中使用v-for循环出来的元素,设置动态id,之后获取下面的所有textarea标签 template: <table cellpadding = 2 v-for="(i ...

  3. Centos7从3.10升级内核到4.9后无法启动解决办法:mpt[23]sas驱动问题

    Centos7升级内核后无法启动解决办法:mpt[23]sas驱动问题 前言 这个问题存在有一段时间了,之前做的centos7的ISO,在进行内核的升级以后就存在这个问题: 系统盘在板载sata口上是 ...

  4. 51nod1085 背包问题【动态规划】

    在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数 ...

  5. Linux启用ftp服务及连接

    虚拟机的系统是centos6.3 第一步.启动ftp service vsftpd restart 提示 vsftpd: 未被识别的服务 解决方法是升级vsftpd服务 yum install vsf ...

  6. 【hihoCoder挑战赛28 A】异或排序

    [题目链接]:http://hihocoder.com/problemset/problem/1509 [题意] [题解] 每次找到相邻两个数的二进制形式中; 不同的最高位; 显然S在这一位必然是确定 ...

  7. java IO(BIO)、NIO、AIO

    IO 服务端ServerSocket 客户端Socket 缺点每次客户端建立连接都会另外启一个线程处理.读取和发送数据都是阻塞式的. 如果1000个客户端建立连接将会产生1000个线程 Server端 ...

  8. 解决Chrome在隐身模式下无法播放Flash视频

    在地址栏输入: chrome://flags/#prefer-html-over-flash 打开如下界面,并设置成如下所示: 然后点击左下方重启按钮: 参考: http://www.cnblogs. ...

  9. QUERY_REWRITE_INTEGRITY

    QUERY_REWRITE_INTEGRITY Property Description Parameter type String Syntax QUERY_REWRITE_INTEGRITY = ...

  10. 问题2-:Syntax error on tokens, delete these tokens

    出现原因:拷贝下来的代码缺少{左大括号 然后运行时run as 没有选到java application 是因为没有main方法 加个public static void main(String() ...