zoukankan      html  css  js  c++  java
  • sleep() 和 wait() 的区别

    好多面经上都出现了,有必要好好熟悉一下

    区别:

      1、wait() 可以指定时间,也可以不指定(等五分钟你进来,或者是不叫你一直等着);sleep()必须指定时间(不能一睡不起)

      2、wait()是Object类中的方法,而sleep()是Thread类中的方法

      3、在同步中,对CPU执行权和锁的处理不同

          wait():释放执行权,释放锁(CPU执行权不释放的话,死机)

          sleep():释放执行权,不释放锁

    有意思的解释,帮助记忆:

      sleep:意思是睡觉,睡觉能够自然醒

      wait:意思是等待,等待的话需要人叫

    代码:参照DreamSea530:加深理解(在原代码基础上加了一句打印语句,更容易理解了)

      先看wait()方法:

      

     1 package com.mianshi.easy;
     2 /**
     3  * Thread sleep和wait区别
     4  * @author DreamSea 
     5  * 2015-7-17
     6  */
     7 public class ThreadTest implements Runnable {
     8     
     9     int number = 10;
    10 
    11     public void firstMethod() throws Exception {
    12        
    13         synchronized (this) {
    14             number += 100;
    15             
    16             System.out.println(number);
    17             
    18         }
    19     }
    20 
    21     public void secondMethod() throws Exception {
    22         synchronized (this) {
    23             /**
    24              * (休息2S,阻塞线程)
    25              * 以验证当前线程对象的机锁被占用时,
    26              * 是否被可以访问其他同步代码块
    27              */
    28             //Thread.sleep(2000);
    29             this.wait(2000);
    30             
    31             number *= 200;
    32             System.out.println(number);
    33         }
    34     }
    35     
    36     public void run() {
    37          
    38         try {
    39             //run()方法中仅调用firstMethod()
    40             firstMethod();
    41         } catch (Exception e) {
    42             e.printStackTrace();
    43         }
    44     }
    45 
    46     public static void main(String[] args) throws Exception {
    47         ThreadTest threadTest = new ThreadTest();
    48         Thread thread = new Thread(threadTest);
    49         //线程进入就绪状态,等待CPU执行
    50         thread.start();
    51         //主线程中的普通方法,此处会首先得到运行
    52         threadTest.secondMethod();
    53     }
    54 }
    View Code

    结果:

    110
    22000

    先打印出110,因为wait()方法会释放锁,所以,子线程可以进入到firstMethod();等待(wait)2s后打印22000,如果此时在firstMethod()打印输出完成后调用this.notify();就可以取消等待的2s

    再看sleep()方法:

     1 package com.mianshi.easy;
     2 /**
     3  * Thread sleep和wait区别
     4  * @author DreamSea 
     5  * 2015-7-17
     6  */
     7 public class ThreadTest implements Runnable {
     8     
     9     int number = 10;
    10 
    11     public void firstMethod() throws Exception {
    12        
    13         synchronized (this) {
    14             number += 100;
    15             
    16             System.out.println(number);
    17             
    18         }
    19     }
    20 
    21     public void secondMethod() throws Exception {
    22         synchronized (this) {
    23             /**
    24              * (休息2S,阻塞线程)
    25              * 以验证当前线程对象的机锁被占用时,
    26              * 是否被可以访问其他同步代码块
    27              */
    28             Thread.sleep(2000);
    29             //this.wait(2000);
    30             
    31             number *= 200;
    32             System.out.println(number);
    33         }
    34     }
    35     
    36     public void run() {
    37          
    38         try {
    39             //run()方法中仅调用firstMethod()
    40             firstMethod();
    41         } catch (Exception e) {
    42             e.printStackTrace();
    43         }
    44     }
    45 
    46     public static void main(String[] args) throws Exception {
    47         ThreadTest threadTest = new ThreadTest();
    48         Thread thread = new Thread(threadTest);
    49         //线程进入就绪状态,等待CPU执行
    50         thread.start();
    51         //主线程中的普通方法,此处会首先得到运行
    52         threadTest.secondMethod();
    53     }
    54 }
    View Code

    结果:

    2000
    2100

    先睡(sleep)2s,这两秒内,没有释放锁,所以子线程无法进入firstMethod();到2s后几乎同时打印2000和2100

  • 相关阅读:
    vue cli 3 构建vue项目
    hadoop综合大作业
    理解Mapreduce
    熟悉常用的HBase操作
    常用的HDFS操作
    爬虫大作业
    数据结构化与保存
    爬取新闻
    网络爬虫基础练习
    Hadoop综合大作业
  • 原文地址:https://www.cnblogs.com/gongxing/p/4654152.html
Copyright © 2011-2022 走看看