Redian新闻
>
[合集] UIC 的经济学怎么样?
avatar
[合集] UIC 的经济学怎么样?# Economics - 经济
X*u
1
帮我LD找的。
希望是以下几个方向的: P2P, smart grid, green Internet and content
distribution.
有兴趣的,给我投个条.
avatar
f*n
2
77th from Java Puzzlers book.
Code is as following:
import java.util.*;
public class Worker extends Thread {
private volatile boolean quittingTime = false;
public void run() {
while (!quittingTime) {
pretendToWork();
}
System.out.println("Beer is good!");
}
private void pretendToWork() {
try {
Thread.sleep(300); // Sleeping on the job?
} catch (InterruptedException ex) {}
}
// It's quitting time, wait for worker - Called by good boss
synchronized void quit() throws InterruptedException {
quittingTime = true;
join();
}
// Rescind quitting time - Called by evil boss
synchronized void keepWorking() {
quittingTime = true;
}
public static void main(String[] args)
throws InterruptedException {
final Worker worker = new Worker();
worker.start();
Timer t = new Timer(true); // Daemon thread
t.schedule(new TimerTask() {
public void run() {
worker.keepWorking();
}
}, 500);
Thread.sleep(400);
worker.quit();
}
}
按分析,Thread.join()调用Object.wait(),解锁了quittingTime,设成false,那么
good boss会进入死循环出不来。
但是在JDK8下,反复运行,没有一次造成死循环。请问为什么。谢谢。
avatar
e*I
3
☆─────────────────────────────────────☆
ceaser (ceaser) 于 (Thu Mar 27 19:55:42 2008) 提到:
PhD,好像是在商学院下面,看系里网页看不出来哪方面比较强,将来就业怎么样呢?有
没有知道的达人可以说说呢?想明年申请试试。
☆─────────────────────────────────────☆
ceaser (ceaser) 于 (Thu Mar 27 20:33:02 2008) 提到:
是不是不太好?前辈们怎么都不说话呢?
☆─────────────────────────────────────☆
Snakeinsuit (蛇蝎心肠) 于 (Thu Mar 27 20:50:22 2008) 提到:
What is UIC?
☆─────────────────────────────────────☆
ceaser (ceaser) 于 (Thu Mar 27 20:51:42 2008) 提到:
sorry, University of Illin
avatar
F*n
4
Because both good & evil bosses are in synchronized block,
The evil boss will not be able to obtain the lock until join() finish.
On the other hand, the worker thread is not in synchronized block and
it can access quittingTime and terminate.

【在 f*n 的大作中提到】
: 77th from Java Puzzlers book.
: Code is as following:
: import java.util.*;
: public class Worker extends Thread {
: private volatile boolean quittingTime = false;
: public void run() {
: while (!quittingTime) {
: pretendToWork();
: }
: System.out.println("Beer is good!");

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