线程中的wait()和sleep()有什么区别?
我是否理解wait()-ing线程仍然处于运行模式并使用CPU周期,但sleep()-ing不消耗任何CPU周期正确?
为什么我们同时使用wait()和sleep()?
它们的实现在较低级别上有什么不同?
线程中的wait()和sleep()有什么区别?
我是否理解wait()-ing线程仍然处于运行模式并使用CPU周期,但sleep()-ing不消耗任何CPU周期正确?
为什么我们同时使用wait()和sleep()?
它们的实现在较低级别上有什么不同?
当前回答
关于睡眠不释放锁,等待释放锁的例子
这里有两个类:
Main:包含Main方法和两个线程。 单例:这是一个单例类,有两个静态方法getInstance()和getInstance(boolean isWait)。 公共类Main { private static singletonA = null; private static Singleton singletonB = null; public static void main(String[] args)抛出InterruptedException { 线程threadA =新线程(){ @Override 公共无效运行(){ singletonA = Singleton.getInstance(true); } }; 线程threadB = new Thread() { @Override 公共无效运行(){ singletonB = Singleton.getInstance(); while (singletonA == null) { system . out。println("SingletonA still null"); } if (singletonA == singletonB) { system . out。println("两个单例是相同的"); }其他{ system . out。println("两个单例不相同"); } } }; threadA.start (); threadB.start (); } }
and
public class Singleton {
private static Singleton _instance;
public static Singleton getInstance() {
if (_instance == null) {
synchronized (Singleton.class) {
if (_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
public static Singleton getInstance(boolean isWait) {
if (_instance == null) {
synchronized (Singleton.class) {
if (_instance == null) {
if (isWait) {
try {
// Singleton.class.wait(500);//Using wait
Thread.sleep(500);// Using Sleep
System.out.println("_instance :"
+ String.valueOf(_instance));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
_instance = new Singleton();
}
}
}
return _instance;
}
}
现在运行这个例子,你会得到以下输出:
_instance :null
Both singleton are same
这里由线程a和线程b创建的单例实例是相同的。这意味着线程b在外面等待,直到线程a释放它的锁。
现在通过注释Thread.sleep(500)来更改Singleton.java;Singleton.class.wait(500);. 这里因为Singleton.class.wait(500);方法threadA将释放所有获取锁并进入“Non Runnable”状态,threadB将获得change进入synchronized块。
现在再运行一次:
SingletonA still null
SingletonA still null
SingletonA still null
_instance :com.omt.sleepwait.Singleton@10c042ab
SingletonA still null
SingletonA still null
SingletonA still null
Both singleton are not same
这里由线程a和线程b创建的单例实例是不一样的,因为线程b得到了进入同步块的变化,500毫秒后,线程a从它的最后一个位置开始,并创建了一个新的单例对象。
其他回答
sleep是Thread的一个方法,wait是Object的一个方法,所以wait/notify是Java中同步共享数据的一种技术(使用monitor),而sleep是线程暂停自身的一种简单方法。
实际上,所有这些都在Java文档中有清楚的描述(但我是在阅读答案后才意识到这一点)。
http://docs.oracle.com/javase/8/docs/api/index.html:
wait() -当前线程必须拥有该对象的监视器。线程释放 该监视器的所有权,并等待直到另一个线程通知 在此对象的监视器上等待的线程可以通过 调用notify方法或notifyAll方法。那么线程 等待,直到它可以重新获得监视器的所有权并恢复执行。
sleep() -根据系统计时器和调度器的精度和准确性,使当前正在执行的线程休眠(暂时停止执行)指定的毫秒数。线程不会失去任何监视器的所有权。
这里wait()将处于等待状态,直到它被另一个线程通知,而sleep()将有一段时间,之后它将自动转移到就绪状态…
这些方法用于不同的事情。
Thread.sleep(5000); // Wait until the time has passed.
Object.wait(); // Wait until some other thread tells me to wake up.
Thread.sleep(n)可以被中断,但是Object.wait()必须被通知。 可以指定等待的最大时间:Object.wait(5000),这样就可以使用wait,呃,睡眠,但这样你就不得不使用锁了。
这两种方法都不会在休眠/等待时使用cpu。
这些方法是使用本地代码实现的,使用类似的结构,但方式不同。
自己寻找:本机方法的源代码是否可用?文件/src/share/vm/prims/jvm.cpp是起点…
Sleep()方法导致当前线程在指定的时间内从运行状态转移到阻塞状态。如果当前线程拥有任何对象的锁,那么它会一直持有它,这意味着其他线程不能在该类对象中执行任何同步方法。
Wait()方法会导致当前线程在指定的时间内进入阻塞状态或直到notify,但在这种情况下,线程会释放对象的锁(这意味着其他线程可以执行调用对象的任何同步方法)。