Object的wait和Thread的sleep
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的更多相关文章
- notification:object not locked by thread before notify()
今天写notification练习时,误将NotificationManager.notify(0, notification);写成notification.notify(); 代码如下 publi ...
- AttributeError: 'module' object has no attribute 'Thread'
$ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last): File "th ...
- 'module' object has no attribute 'Thread'解决方法及模块加载顺序
源码片段: class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Threa ...
- 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 ...
- 为什么等待和通知是在 Object 类而不是 Thread 中声明的?
一个棘手的 Java 问题,如果 Java编程语言不是你设计的,你怎么能回答这个问题呢.Java编程的常识和深入了解有助于回答这种棘手的 Java 核心方面的面试问题.为什么 wait,notify ...
- java之Thread.sleep(long)与object.wait()/object.wait(long)的区别及相关概念梳理(good)
一.Thread.sleep(long)与object.wait()/object.wait(long)的区别sleep(long)与wait()/wait(long)行为上有些类似,主要区别如下:1 ...
- 多线程爬坑之路-Thread和Runable源码解析
多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...
- Android笔记——Handler Runnable与Thread的区别
在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...
- java 多线程 Synchronized方法和方法块 synchronized(this)和synchronized(object)的理解
synchronized 关键字,它包括两种用法:synchronized 方法和 synchronized 块. 1. synchronized 方法:通过在方法声明中加入 synchronized ...
随机推荐
- SSL证书在线申请和取回证书指南
1.客服下单后用户会收到一封邮件,来验证接收证书的邮箱;如图1所示:只有完成此邮箱验证才能正常申请证书; 请注意:为了确保您或您的用户能正常收到WoSign证书管理系统自动发送的邮件,请一定要把系统发 ...
- Java 8 集合不完全一览
JDK 8 List 名称 线程安全 数据结构 允许 null 默认初始容量 扩容策略 备注 ArrayList 不安全 数组 允许 10 1.5 * old LinkedList 不安全 双链表 允 ...
- php第五节课
封装 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- jQuery cxSelect 联动下拉菜单
插件简介 cxSelect 是基于 jQuery 的多级联动菜单插件,适用于省市.商品分类等联动菜单. 列表数据通过 AJAX 获取,也可以自定义,数据内容使用 JSON 格式. 同时兼容 Zepto ...
- (12)Spring Boot改变JDK编译版本【从零开始学Spring Boot】
Spring Boot在编译的时候,是有默认JDK版本的,如果我们期望使用我们要的JDK版本的话,那么要怎么配置呢? 这个只需要修改pom.xml文件的<build> -- <plu ...
- LightOJ1234 Harmonic Number
/* LightOJ1234 Harmonic Number http://lightoj.com/login_main.php?url=volume_showproblem.php?problem= ...
- poj 3469 最小割模板sap+gap+弧优化
/*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/ #include<stdio.h> #include<string.h> #include<queue> ...
- 洛谷——P1060 开心的金明
https://www.luogu.org/problem/show?pid=1060#sub 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的 ...
- 日志输出最不重要的就是控制台输出,控制台输出就是system.out而已
1.日志输出最不重要的就是控制台输出,控制台输出就是system.out而已 2.所以日志输出时候会存在一个Bug就是:stdout要配置在日志输出的最前面,因为stdout控制台输出,最不重要,如果 ...
- ZOJ 3209
精确覆盖 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...