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 ... 
随机推荐
- 复习MySQL②数据类型及约束条件
			数据类型分为数值类型.日期和时间类型.字符串类型 数值类型: – INT:有符号的和无符号的.有符号大小-2147483648~2147483647,无符号大0~4294967295. 宽度最多为11 ... 
- Labview学习笔记(二)
			一.编程基础 LABVIEW程序成为虚拟.仪器程序,简称VI,一个最基本的VI包括三个部分:前面板.程序框图和图标/连接端口. 1.前面板 在前面板窗口中,可以添加输入控件和显示控件,同时,可以用快捷 ... 
- python 遍历xml所有节点
			1.xml文件 2.代码 #coding:utf-8 import xml import xml.etree.ElementTree as ET """ 实现从xml文件 ... 
- Linux基础:find命令总结
			本文只总结一些常用的用法,更详细的说明见man find和 info find. find命令 find命令常用来查找文件或目录,可以根据给定的路径和表达式查找所需的文件或目录.该工具是由findut ... 
- grunt入门    出处:http://artwl.cnblogs.com
			grunt-contrib-uglify uglify是一个文件压缩插件,项目地址:https://github.com/gruntjs/grunt-contrib-uglify 本文将以一个DEMO ... 
- 函数(day08)
			C语言里可以采用分组的方式管理语句 每个语句分组叫做一个函数 多函数程序执行的时候时间分配情况必须 遵守以下规则 .整个程序的执行时间被划分成几段,每段 时间都被分配给一个函数使用 .不同时间段不能互 ... 
- android  debug签名文件
			现象 可以运行程序,但不能启动安装成功的软件 并且run application的时候也不弹出界面. 路径: C:\Users\sunfb\.android 下替换debug.keystore 就OK 
- Mysql查询缓存研究
			转载声明:本文为DBA+社群原创文章,转载必须连同本订阅号二维码全文转载,并注明作者名字及来源:DBA+社群(dbaplus). http://mp.weixin.qq.com/s?__biz=MzI ... 
- linux下安装rar解压包
			直接解压时出现的问题如下 原因:使用rar命令需要安装WinRAR 1.在本机下载好解压,然后将解压包拖到linux上 2.进行安装,在rar目录想直接make 
- 如何做URL静态化 和页面的静态化
			为什么要进行URL静态化? 如果帮到了您,您可以小支持一下,谢谢您 1.更好的迎合搜索引擎工作原理的爬行抓取机制:2.把网站URL静态化更有助于网站获得好的排名:3.URL静态化有利于用户体验.不 ... 
