java线程之——sleep()与wait()的区别
sleep()是Thread的方法,wait()是Object的方法
如果线程进入了同步锁,sleep不会释放对象锁,wait会释放对象锁
sleep的作用就是让正在执行的线程主动让出CPU,给其它线程获得CPU的机会,在sleep指定的时间之后,CPU才会回到这个线程上继续往下执行,当线程进入了同步锁时,当别的线程也需要被加锁的资源时,sleep方法即使让出了CPU,别的线程也无法执行,因为无法获得锁。
wait方法是指在一个已经进入了同步锁的线程内,让自己暂时让出同步锁给别的线程用,只有等其他线程调用了notify或notifyAll方法后,才能去获得同步锁继续执行,需要注意的是,notify并不会释放锁,只是告诉调用过wait方法的线程可以去争取锁了,而不是马上得到锁。
此外在使用wait,notify,notifyAll方法的时候,要注意只能使用对象锁的持有者,即被封锁的对象来调用,不然会出IllegalMonitorStateException异常。
package javaBase;
/**
* sleep()和wait()的区别
* sleep()是Thread的方法,wait()是Object的方法
* sleep不会释放封锁的资源,wait会释放封锁的资源
* @author cnxno1
*
*/
public class JB_047_SleepAndWait {
public static void main(String[] args) {
Thread thread1 = new Thread(new Thread1());
Thread thread2 = new Thread(new Thread2());
thread1.start(); try {
System.out.println("延迟5秒启动线程2");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} thread2.start();
}
}
/**
* 公共资源类
* @author cnxno1
*
*/
class Resource{
public static String resource = "public resource";
}
/**
* 调用wait方法的线程
* @author cnxno1
*
*/
class Thread1 implements Runnable{
@Override
public void run(){
synchronized(Resource.resource){
System.out.println("this is thread1 , i hava the "+Resource.resource);
System.out.println("this is thread1 , i'm waiting the "+Resource.resource);
try{
Resource.resource.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("this is thread1, i will going on...");
System.out.println("this is the end of thread1.");
}
}
} /**
* 调用sleep方法的线程
* @author cnxno1
*
*/
class Thread2 implements Runnable{
@Override
public void run(){
synchronized(Resource.resource){
System.out.println("this is thread2 , i'm notifyAll thread...");
Resource.resource.notifyAll();
System.out.println("this is thread2 , i will sleep 10 seconds...");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("this is thread2, i will going on...");
System.out.println("this is the end of thread2.");
}
}
}
运行的结果如下:
延迟5秒启动线程2
this is thread1 , i hava the public resource
this is thread1 , i'm waiting the public resource
this is thread2 , i'm notifyAll thread...
this is thread2 , i will sleep 10 seconds...
this is thread2, i will going on...
this is the end of thread2.
this is thread1, i will going on...
this is the end of thread1.
可以看到thread1在调用了wait方法之后,thread2获得了同步锁,但是在thread2调用了sleep方法后,thread1并没有获得同步锁执行下去,而是在thread2 sleep了10秒执行完之后才获得了Resource.resource的锁得以执行完成。
java线程之——sleep()与wait()的区别的更多相关文章
- java线程中的sleep和wait区别
面试题:java线程中sleep和wait的区别以及其资 ...
- java 线程同步 原理 sleep和wait区别
java线程同步的原理java会为每个Object对象分配一个monitor, 当某个对象(实例)的同步方法(synchronized methods)被多个线程调用时,该对象的monitor将负责处 ...
- JAVA线程池shutdown和shutdownNow的区别
一.区别介绍 shutDown() 当线程池调用该方法时,线程池的状态则立刻变成SHUTDOWN状态.此时,则不能再往线程池中添加任何任务,否则将会抛出RejectedExecutionExcept ...
- 【转】java线程系列---Runnable和Thread的区别
在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...
- java线程interrupt、interrupted 、isInterrupted区别
前言 在分析interrupt之前,应该先了解java里线程有5种状态,其中有一个阻塞状态,interrupt和阻塞有关. interrupt() 方法 作用于要中断的那个线程. interrupt( ...
- java线程(上)Thread和Runnable的区别
首先讲一下进程和线程的区别: 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程. 线程:同一类线程共享代码和数据空间,每个线程有独立的运行栈 ...
- java线程系列---Runnable和Thread的区别 (转载)
转自:http://blog.csdn.net/wwww1988600/article/details/7309070 在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Run ...
- Java线程 - sleep()和wait()方法的区别, 线程阻塞BLOCKED和等待WAITING的区别
一. sleep()和wait()方法的区别 sleep()方法 sleep()方法是Thread类的方法,通过其定义可知是个native方法,在指定的时间内阻塞线程的执行.而且从其注释中可知,并不会 ...
- java线程中start和run的区别
public class Test1 extends Thread { @Override public void run() { while (true) { System.out.println( ...
随机推荐
- 一个静态的HTML页面用jquery ajax登录到sharepoint页面
$.ajax({ type: "get", url: "http://", d ...
- offset求结构体成员的偏移量
[代码] C++ Code 12345678910111213141516171819202122232425262728293031 /* version: 1.0 author: hell ...
- 华为 MATE7 调试 LOCAT 日志不输出问题
[转]华为 MATE7 调试 LOCAT 日志不输出问题 http://www.cnblogs.com/glaivelee/p/4593221.html 用手机进行调试,在电脑上不显示logcat信息 ...
- 5.js模式-职责链模式
1. 职责链模式 将对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. var chain = function(fn){ this.fn = fn; this.successor = ...
- Python批量修改文件名-后缀
LyncLynn用途: 批量修改文件格式,文件名后缀. #Version: V1.0 #Author:lynclynn #Description:Change the filename #Create ...
- [转] 理解 Thread.Sleep 函数
原文链接:http://www.cnblogs.com/ILove/archive/2008/04/07/1140419.html 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时 ...
- 【leetcode】Jump Game I & II (hard)
Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- iOS进阶面试题----多线程
1 多线程是什么 多线程是个复杂的概念,按字面意思是同步完成多 项任务,提高了资源的使用效率,从硬件.操作系统.应用软件不同的角度去看,多线程被赋予不同的内涵,对于硬件,现在市面上多数的CPU都是多核 ...
- springMVC创建基础变量
在springMVC中有一些变量是基础变量,可以在全局多个地方使用,在修改规则的时候,这样只用修改一个地方就好了,而且可以避免很多不必要的bug出现下面就来总结一下在我的项目中如何去创建一个全局基础变 ...