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. 【leetcode】Reverse Nodes in k-Group

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...

  2. java web 学习 --第十天(Java三级考试)

    第九天学习内容:http://www.cnblogs.com/tobecrazy/p/3470195.html 需求概述 创建程序模块,读取并显示数据库中的书籍信息.当前有如下需求:需要显示数据库中所 ...

  3. Django ~module index

    https://docs.djangoproject.com/en/1.9/py-modindex/ django.conf.urls django.contrib.admin django.db.m ...

  4. [Android] RelativeLayout, LinearLayout,FrameLayout

    Android RelativeLayout 属性 // 相对于给定ID控件 android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below ...

  5. java获取本月或某月的第一天和最后一天

    获取某月的第一天和最后一天的日期 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Ca ...

  6. 微信video标签全屏无法退出bug 本文系转载

    安卓(android)微信里面video播放视频,会被强制全屏,播放完毕后还有腾讯推荐的视频,非常讨厌..强制被全屏无法解决,但是视频播放完毕后退出播放器可以解决.方法就是视频播放完毕后,用音频aud ...

  7. java中带继承类的加载顺序详解及实战

    一.背景: 在面试中,在java基础方面,类的加载顺序经常被问及,很多时候我们是搞不清楚到底类的加载顺序是怎么样的,那么今天我们就来看看带有继承的类的加载顺序到底是怎么一回事?在此记下也方便以后复习巩 ...

  8. Java Collection、Map集合总结

    1.Java中的Collection集合总结 Collection |---List(存储有序,可重复) |---ArrayList 底层数据结构是数组,查询快,增删慢. 线程不安全.效率高 |--- ...

  9. 不同版本CUDA编程的问题

    1 无法装上CUDA的toolkit 卸载所有的NVIDIA相关的app,包括NVIDIA的显卡驱动,然后重装. 2之前的文件打不开,one or more projects in the solut ...

  10. rabbitmq_config

    https://github.com/rabbitmq/rabbitmq-server/blob/stable/docs/rabbitmq.config.example   %% ---------- ...