Topic: 关于线程,同步抢先的问题

  Print this page

1.关于线程,同步抢先的问题 Copy to clipboard
Posted by: ycxct
Posted on: 2006-12-24 17:01

如下代码,线程A离开同步后,B和C哪个能抢到同步快的使用权呢,本来我确定应该是随机的,但是做多了题目后开始犯晕了...哪位高手能解答一下...
 
class MyThread extends Thread {
MyThread(String name) {
super(name);
}

public void run() {
sync.go();
System.out.println(this.getName());
}
}

class sync {
synchronized static void go() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}

public class test {
public static void main(String[] args) {
new MyThread("A").start();
System.out.println(new Date().getTime());
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
new MyThread("B").start();
new MyThread("C").start();
}
}


2.Re:关于线程,同步抢先的问题 [Re: ycxct] Copy to clipboard
Posted by: cxp108
Posted on: 2006-12-25 09:07

我认为答案应该是:

JVM不保证线程B运行于线程C之前。

但是一般情况下B会在C之前。

3.Re:关于线程,同步抢先的问题 [Re: ycxct] Copy to clipboard
Posted by: ycxct
Posted on: 2006-12-25 22:32

哦,清醒了,都是被考试害的,再次谢谢你的解答哦

4.Re:关于线程,同步抢先的问题 [Re: ycxct] Copy to clipboard
Posted by: ranchgirl
Posted on: 2007-01-07 18:11

It might be OS dependent, which means you might get different result on different OS.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923