Thread.sleep()在java中同步 [英] Thread.sleep() with synchronization in java

查看:477
本文介绍了Thread.sleep()在java中同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当调用Thread.sleep(10000)时,当前线程将进入休眠状态。
如果在同步方法中调用Thread.sleep(10000)是否其他线程可以在该时间段内执行?

when Thread.sleep(10000) is invoked current Thread will go to sleeping state. If Thread.sleep(10000) is invoked in synchronization method whether other thread can execute in that period?

推荐答案

如果您在同步方法或块中执行 Thread.sleep(10000),则释放锁定。因此,如果其他线程正在等待该锁定,则它们将无法执行。

If you do Thread.sleep(10000) within a synchronized method or block you do not release the lock. Hence if other Threads are waiting on that lock they won't be able to execute.

如果您想等待指定的时间来发生某种情况,释放你需要使用的对象锁 Object.wait(long)

If you want to wait for a specified amount of time for a condition to happen and release the object lock you need to use Object.wait(long)

这篇关于Thread.sleep()在java中同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆