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()的区别的更多相关文章

  1. java线程中的sleep和wait区别

                                                                            面试题:java线程中sleep和wait的区别以及其资 ...

  2. java 线程同步 原理 sleep和wait区别

    java线程同步的原理java会为每个Object对象分配一个monitor, 当某个对象(实例)的同步方法(synchronized methods)被多个线程调用时,该对象的monitor将负责处 ...

  3. JAVA线程池shutdown和shutdownNow的区别

    一.区别介绍 shutDown()  当线程池调用该方法时,线程池的状态则立刻变成SHUTDOWN状态.此时,则不能再往线程池中添加任何任务,否则将会抛出RejectedExecutionExcept ...

  4. 【转】java线程系列---Runnable和Thread的区别

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

  5. java线程interrupt、interrupted 、isInterrupted区别

    前言 在分析interrupt之前,应该先了解java里线程有5种状态,其中有一个阻塞状态,interrupt和阻塞有关. interrupt() 方法 作用于要中断的那个线程. interrupt( ...

  6. java线程(上)Thread和Runnable的区别

    首先讲一下进程和线程的区别: 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程. 线程:同一类线程共享代码和数据空间,每个线程有独立的运行栈 ...

  7. java线程系列---Runnable和Thread的区别 (转载)

    转自:http://blog.csdn.net/wwww1988600/article/details/7309070 在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Run ...

  8. Java线程 - sleep()和wait()方法的区别, 线程阻塞BLOCKED和等待WAITING的区别

    一. sleep()和wait()方法的区别 sleep()方法 sleep()方法是Thread类的方法,通过其定义可知是个native方法,在指定的时间内阻塞线程的执行.而且从其注释中可知,并不会 ...

  9. java线程中start和run的区别

    public class Test1 extends Thread { @Override public void run() { while (true) { System.out.println( ...

随机推荐

  1. QQ登录类

    2015-3-31 22:02:09 (同一套代码, pc端不能登录, 但是, 手机和平板都可以正常登录.....) 1. 首先是库文件, 登录->授权->token->openid ...

  2. FastReport调用Delphi中的自定义函数(人民币大写金额)mtm

    1. 在 FormCreate 中向FastReprot添加函数 (fPrint)窗口 procedure TfPrint.FormCreate(Sender: TObject); frxReport ...

  3. java读取excel

    /* * this function will read from excel * and will return the items of excel */ public static String ...

  4. ACM/ICPC 之 快排+归并排序-记录顺序对(TSH OJ-LightHouse(灯塔))

    TsingHua OJ 上不能使用<algorithm>头文件,因此需要手写快排(刚开始写的时候自己就出了很多问题....),另外本题需要在给横坐标排序后,需要记录纵坐标的顺序对的数量,因 ...

  5. ffmpeg-20160522-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  6. Linux/Unix命令

    MAC 中自定义环境变量 打开:nano .bash_profile 查看:cat text 保存退出:Ctrl+C,Y #在.bash_profile 中添加tree alias tree=&quo ...

  7. MST:Bad Cowtractors(POJ 2377)

    坏的牛圈建筑 题目大意:就是现在农夫又要牛修建牛栏了,但是农夫想不给钱,于是牛就想设计一个最大的花费的牛圈给他,牛圈的修理费用主要是用在连接牛圈上 这一题很简单了,就是找最大生成树,把Kruskal算 ...

  8. BestCoder36 1002.Gunner 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5199 题目意思:给出鸟在树上的高度,以及射击到的高度,问每次射击能射中鸟的数量 用 vector 里面 ...

  9. How can I fix “Compilation unit name must end with .java, or one of the registered Java-like extensions”?

    How can I fix “Compilation unit name must end with .java, or one of the registered Java-like extensi ...

  10. 自定义循环滑动的viewpager

    今天和大家分享一下如何定制一个可以循环滑动的viewpager.其实今天更重要的提供一种组件化思想,当然你可以理解为面向对象思想. 吐槽一下网上流行的实现方式吧(为了方便说明,下文称之为方式A),方式 ...