Threed.sleep是不会释放锁,而wait是释放锁的(对象锁)

  实战分析
  
  一直都说,Threed.sleep是不会释放锁,而wait是释放锁的(对象锁),现理论上来分析一下啊。
  
  v
  
  package thread.concurrent;
  
  public class DeepenSleep implements www.wmyl11.com/ Runnable {
  
  private int number = 10;
  
  public void firstMethod() throws Exception {
  
  synchronized (this) {
  
  System. out.println("in first method");
  
  number += 100;
  
  System. out.println("+100=" + number);
  
  }
  
  }
  
  public void secondMethod() throws www.wmyl15.com/ Exception {
  
  synchronized (this) {
  
  System. out.println("in second method, www.wmyl166.cn prepare sleep");
  
  /**
  
  * (休息2S,阻塞线程) 以验证当前线程对象的机锁被占用时, 是否被可以访问其他同步代码块
  
  */
  
  Thread. sleep(2000);
  
  System. out.println("wake up!!");
  
  // this.wait(2000);
  
  number *= 200;
  
  System. out.println("*200=" + number);
  
  }
  
  }
  
  @Override
  
  public void run() {
  
  try {
  
  System. out.println(" www.wmyl119.cn run thread...");
  
  firstMethod();
  
  } catch (Exception e) {
  
  e.printStackTrace();
  
  }
  
  }
  
  public static void main(String[] args) throws Exception {
  
  DeepenSleep dt = new DeepenSleep();
  
  Thread thread = new Thread(dt);
  
  thread.start();
  
  System. out.println("prepare run second method");
  
  dt.secondMethod();

  输出如下:
  
  执行结果
  
  分析:主线程启动起来,因为创建线程等的资源消耗,所以主线程会先执行 dt.secondMethod(),因此会先输出prepare run second method,其后执行secondMehtod方法(注意该方法是要先闹到锁对象),而该方法直接将线程睡眠2s(注意此处对象锁DeepenSleep的实例对象并没有释放),然后执行线程dt的run方法,该方刚发执行dt的firstMethod,然而,该方法也是需要获取锁对象的,而此时他没先不能获取到,因为secondMehtod没有释放锁(准确点讲,主线程没有释放锁);然后等了2s,主线程睡眠时间已过,他warkup之后,因为还拥有锁,因此直接run secondMethod的剩下的方法,先输出”wake up”,然后执行 number*200,执行完,主线程释放掉锁,而dt线程拿到锁,执行run方法,拿到锁,执行run方法的synchronized的剩余方法:先输出”in first method”,然后执行加100的操作。
  
  我们来变一下将firstMethod的同步去掉,看输出是什么样子
  
  package thread.concurrent;
  
  public class DeepenSleep implements Runnable {
  
  private int number = 10;
  
  public void firstMethod() throws Exception {
  
  // synchronized (this) {
  
  System. out.println( "in first method");
  
  number += 100;
  
  System. out.println( "+100=" + number);
  
  // }
  
  }
  
  public void secondMethod() throws Exception {
  
  synchronized ( this) {
  
  System. out.println( "in www.wmyl110.com second method, prepare sleep");
  
  /**
  
  * (休息2S,阻塞线程) 以验证当前线程对象的机锁被占用时, 是否被可以访问其他同步代码块
  
  */
  
  Thread. sleep(2000);
  
  System. out.println( "wake up!!");
  
  // this.wait(2000);
  
  number *= 200;
  
  System. out.println( "*200=" + number);
  
  }
  
  }
  
  @Override
  
  public void run() {
  
  try {
  
  System. out.println( "run thread...");
  
  firstMethod();
  
  } catch (Exception e) www.chuangyed.com {
  
  e.printStackTrace();
  
  }
  
  }
  
  public static void main(String[] args) www.xingchexiu.com/ throws Exception {
  
  DeepenSleep dt = new DeepenSleep();
  
  Thread thread = new Thread(dt);
  
  thread.start();
  
  System. out.println( "prepare run second method");
  
  dt.secondMethod();
 
  输出如下:
  
  执行结果
  
  分析:不同点在于,主线程睡眠之后,没有释放锁,dt线程执行firstMethod并不需要锁,因此先run firstMethod中的逻辑,先加100,然今,主线程睡醒之后,再执行剩下的逻辑,乘以200。
  
  Thread.sleep(1000),1000ms后是否立即执行?
  
  不一定,在未来的1000毫秒内,线程不想再参与到CPU竞争。那么1000毫秒过去之后,这时候也许另外一个线程正在使用CPU,那么这时候操作系统是不会重新分配CPU的,直到那个线程挂起或结束;况且,即使这个时候恰巧轮到操作系统进行CPU 分配,那么当前线程也不一定就是总优先级最高的那个,CPU还是可能被其他线程抢占去。
  
  Thread.sleep(0),是否有用?
  
  boss:“给你睡0小时”。
  
  coder:“你TM逗我啊”。
  
  休眠0ms,这样的休眠有何意义?Thread.Sleep(0)的作用,就是“触发操作系统立刻重新进行一次CPU竞争,重新计算优先级”。竞争的结果也许是当前线程仍然获得CPU控制权,也许会换成别的线程获得CPU控制权。这也是我们在大循环里面经常会写一句Thread.sleep(0) ,因为这样就给了其他线程比如Paint线程获得CPU控制权的权力,这样界面就不会假死在那里。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值