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. ODATA 云驱动 http://www.cdata.com/cloud/

    ODATA 云驱动   http://www.cdata.com/cloud/    目前支持:ORACLE.MS SQL . MYSQL. -------------- rssbus      ht ...

  2. 【leetcode】N-Queens

    N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...

  3. Light OJ 1140

    数位dp,需要记录前导0. 数位dp中需要注意统计0,00,000……这些数字. 数位dp的写法可以分为两类.由于我们通常采用记忆化搜索的方式进行dp,所以我们有一个记忆化数组. 一种是记忆化数组的意 ...

  4. vSphere Client无法连接到服务器 出现未知错误的解决方法

    VMware ESXi服务器虚拟机在正常使用过程中,有时候会突然出现远程连接不上的问题,那么这个时候使用vSphere Client连接会出现如下错误: 虽然连接不上,但是可以ping通,所以分析有可 ...

  5. Python 之 【re模块的正则表达式学习】

    摘要: re模块包括操作正则表达式的函数,一些工作中都需要用到,现在说明下使用方法. 使用说明: 一,re模块下的函数:            函数             描述 compile(pa ...

  6. ada 图形编辑器 - GNAT GPL

    The GNAT GPL and SPARK GPL Editions are made available to the free software developers by AdaCore. T ...

  7. VC++ 模块与资源分离

    在一些开发过程中,需要模块支持中英文语言切换,比较好的实现方式是从模块中将资源分离出来,做成中英文两个资源dll,根据需要加载不同的dll从而实现切换不同的语言显示. 新建一个资源dll文件,选择Wi ...

  8. mybatis 的if else

    <update id="update" parameterType="XXX">         update XX set YY          ...

  9. 【编程题目】输出 1 到最大的 N 位数

    65.输出 1 到最大的 N 位数(运算)题目:输入数字 n,按顺序输出从 1 最大的 n 位 10 进制数.比如输入 3,则输出 1.2.3 一直到最大的 3 位数即 999. 思路:肯定要考虑数字 ...

  10. 建立controller

    复制controller,重建controller 改: @Controller("[productController]") @RequestMapping("/[pr ...