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. dpkg:处理软件包 mysql-server-5.5 (--configure)时出错

        卸载MySQL重新安装会出现如下问题:出现该问题主要是安装MySQL前需要删除 /var/lib/mysql文件夹以及/etc/mysql文件夹执行命令:    sudo rm /var/li ...

  2. res对象json,redirect

    1.res.json() var express=require('express'); var app=express(); app.get('/',function(req,res){ //返回j ...

  3. HLPP算法 一种高效的网络最大流算法

    #include <algorithm> #include <cstdio> #include <cctype> #include <queue> #d ...

  4. Network-Flow

    //Created by pritry int graph[MAX][MAX]; //原图 int source; //起点,这里为0 int sink; //终点,这里为n-1 int e[MAX] ...

  5. [luogu4285 SHOI2008] 汉诺塔 (暴力,数学)

    传送门 Solution 强行猜测公式形如\(f_i=k\times f_{i-1}+b\),暴力求\(f_1,f_2,f_3\),剩下的递推就行 Code #include <cstdio&g ...

  6. mysql查询表里的重复数据

    先贴个简单的SQL语句 select username,count(*) as count from hk_test group by username having count>1; 使用详情 ...

  7. 系统和帮助-Linux基础知识

    iOS镜像: 硬盘分区:留出一些空间;实在不成,可安装完成以后,新增一块虚拟硬盘; 终端:terminal 用户界面: GUI:图形界面 GNome KDE CLI: bash,zsh,sh,csh, ...

  8. windows 下关于nginx的操作

    在nginx安装目录下操作 1.启动:start nginx 2.停止:① nginx.exe -s stop ②nginx.exe -s quit   注:stop是快速停止nginx,可能并不保存 ...

  9. Redis启动出错 noauth authentication required.

    输入认证过的密码即可. 在命令行中运行: auth password

  10. [bzoj1500][NOI2005]维修数列_非旋转Treap

    维修数列 bzoj-1500 NOI-2005 题目大意:给定n个数,m个操作,支持:在指定位置插入一段数:删除一个数:区间修改:区间翻转.查询:区间和:全局最大子序列. 注释:$1\le n_{ma ...