在java中的线程实例上调用wait() [英] Calling wait() on Thread instance in java

查看:47
本文介绍了在java中的线程实例上调用wait()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们在 Thread 类的实例上调用 wait() 方法会发生什么.

What will happen if we call wait() method on instance of Thread class.

Thread t1 = new MyThread();
t1.wait();

我的线程 t1 的状态是什么?

What will be the state of my thread t1?

推荐答案

这将与您调用 wait() 之前相同.wait()java.lang.Object 的一个方法.它等待与对象关联的内部监视器.在这种情况下,对象是一个线程,这不会改变 wait() 的作用.不过,使用 Thread 对象作为监视器非常奇怪.您应该使用私有和最终对象作为监视器.

It will be the same as before your call to wait(). wait() is a method of java.lang.Object. It waits on the intrinsic monitor associated to the object. In that case, the object is a thread, and that doesn't change what wait() does. It's pretty bizarre to use a Thread object as a monitor, though. You should use a private and final object as monitor.

这篇关于在java中的线程实例上调用wait()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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