Redian新闻
>
线程hardy会一直等待下去么?
avatar
线程hardy会一直等待下去么?# Java - 爪哇娇娃
q*s
1
代码见下面,为什么?
多谢各位!
public class TwoThreads {
static Thread laurel, hardy;
static final Object[] locker = new Object[0];
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
avatar
q*u
2
hardy.sleep(1000); 等效于Thread.sleep(1000); 所以laurel 肯定先结束,最后两
个字母一定是CF
你这个程序是不是copy错了,locker在哪里

代码见下面,为什么?
多谢各位!
public class TwoThreads {
static Thread laurel, hardy;
static final Object[] locker = new Object[0];
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.

【在 q***s 的大作中提到】
: 代码见下面,为什么?
: 多谢各位!
: public class TwoThreads {
: static Thread laurel, hardy;
: static final Object[] locker = new Object[0];
: public static void main(String[] args) {
: laurel = new Thread() {
: public void run() {
: System.out.println("A");
: try {

avatar
m*t
3
This is interesting. Something must have called laurel.notify() or notifyAll
()
for hardy to exit. I just don't understand what.

【在 q***s 的大作中提到】
: 代码见下面,为什么?
: 多谢各位!
: public class TwoThreads {
: static Thread laurel, hardy;
: static final Object[] locker = new Object[0];
: public static void main(String[] args) {
: laurel = new Thread() {
: public void run() {
: System.out.println("A");
: try {

avatar
q*s
4
程序没有错,你说的结果也是对的,hardy并没有一直等下去。程序输出的结果为:ADCF。
本来我认为hardy会一直等下去的,因为那里有一个wait(),但是,结果没有。
但如果我把wait函数synchronized到另外一个对象上,比如那个locker,然后用locker.wait()来等待,则hardy会一直等下去。
哪位大侠能给解释一下,多谢!

【在 q*********u 的大作中提到】
: hardy.sleep(1000); 等效于Thread.sleep(1000); 所以laurel 肯定先结束,最后两
: 个字母一定是CF
: 你这个程序是不是copy错了,locker在哪里
:
: 代码见下面,为什么?
: 多谢各位!
: public class TwoThreads {
: static Thread laurel, hardy;
: static final Object[] locker = new Object[0];
: public static void main(String[] args) {

avatar
q*s
5
这也是我不明白的地方,没有notify,wait是如何结束的?

notifyAll

【在 m******t 的大作中提到】
: This is interesting. Something must have called laurel.notify() or notifyAll
: ()
: for hardy to exit. I just don't understand what.

avatar
m*t
6

Since you've tried locker and it doesn't happen there, it's possible
that this is thread implementation specific, i.e., when a thread is done
executing, some code calls notify() or more likely notifyAll().
I looked for that in Thread's source code but a lot of it is
native.

【在 q***s 的大作中提到】
: 这也是我不明白的地方,没有notify,wait是如何结束的?
:
: notifyAll

avatar
s*n
7
very interesting. since spurious wakeups are always allowed for wait(), it
is not wrong that JVM decides to wake it up upon thread termination. this
cannot be a useful feature, nobody should sync on a thread object like you
did. probably for some historic reasons.
avatar
q*u
8
其实是有这方面的应用的,比如
synchronized(this){
...
}
这种代码出现频率很高的。
看来如果是thread对象, 猜测那么jvm有这个特性,当线程结束,线程自己会自动
notify了

very interesting. since spurious wakeups are always allowed for wait(), it
is not wrong that JVM decides to wake it up upon thread termination. this
cannot be a useful feature, nobody should sync on a thread object like you
did. probably for some historic reasons.

【在 s******n 的大作中提到】
: very interesting. since spurious wakeups are always allowed for wait(), it
: is not wrong that JVM decides to wake it up upon thread termination. this
: cannot be a useful feature, nobody should sync on a thread object like you
: did. probably for some historic reasons.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。