由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 问个Thread 的问题,java certificate里的
相关主题
线程问题。can applet implements runnable?
一道java题Help with Tomcat for Eclipse v1.03
ZT: 关于性爱的多线程问题研究(一)Re: How to showStatus in Applet?
能这么 create thread 吗?FrameWork of Thread application 1
implements runable 和 extends threadRe: connection pool
好虫,exploit等大牛请进--关于htmlunit●●●●紧急求助JAVA初级问题,今天project due●●●●
weird class definitionCan Java thread return a value?
Java练习题 6几个问题
相关话题的讨论汇总
话题: thread话题: public话题: new话题: void话题: what
进入Java版参与讨论
1 (共1页)
o*a
发帖数: 229
1
1. public class X implements Runnable{
2. private int x;
3. private int y;
4.
5. public static void main(String[]args){
6. X that = new X();
7. (new Thread(that)).start();
8. (new Thread(that)).start();
9. }
10.
11. public void run(){
12. for (;;){
13. x++;
14. y++;
15. System.out.printIn(“x=” + x + “, y = ” + y); 16. }
17. }
18.}
What is the result?
A. Errors at lines 7 and 8 cause compilation to fail.
B. The program prints pairs of values for x and y that might not always be the
same on the sa
J*****a
发帖数: 12
2
记得这个问题有争论,win和linux的线程模式不一样,你换到后者上面
执行就会结果不同了
在win上肯定是d,原因就是两个线程级别相同...

【在 o*a 的大作中提到】
: 1. public class X implements Runnable{
: 2. private int x;
: 3. private int y;
: 4.
: 5. public static void main(String[]args){
: 6. X that = new X();
: 7. (new Thread(that)).start();
: 8. (new Thread(that)).start();
: 9. }
: 10.

n*****k
发帖数: 123
3

the
on
on
only
x++
y
I am pretty sure the key is wrong.
You can varify that by either adding
if (x! = y)
System.out.println("x = " + x + " y = " + y);
insider the run method.
or Thread.currentThread().sleep(100); between x ++ and y++,
You will see that x and y will have difffernt value.
Hope this will help.

【在 o*a 的大作中提到】
: 1. public class X implements Runnable{
: 2. private int x;
: 3. private int y;
: 4.
: 5. public static void main(String[]args){
: 6. X that = new X();
: 7. (new Thread(that)).start();
: 8. (new Thread(that)).start();
: 9. }
: 10.

m******t
发帖数: 2416
4

I believe it has more to do with the facts that both threads
are started almost immediately back to back, that they run
on exactly the same code, and that there probably wasn't a lot
of other things running when this test program was executed.
If you insert a random sleep between
x++ and y++, it'll make some difference.

【在 J*****a 的大作中提到】
: 记得这个问题有争论,win和linux的线程模式不一样,你换到后者上面
: 执行就会结果不同了
: 在win上肯定是d,原因就是两个线程级别相同...

J*****a
发帖数: 12
5
if you insert some random sleeps, i should be different, but it's not
the same env.
the orginal is the circle time is what jvm excute x++ and y++, and
the later should plus a random paremeter.
in my remember, one of win and linux's thread mode is 抢占式, so ...

【在 m******t 的大作中提到】
:
: I believe it has more to do with the facts that both threads
: are started almost immediately back to back, that they run
: on exactly the same code, and that there probably wasn't a lot
: of other things running when this test program was executed.
: If you insert a random sleep between
: x++ and y++, it'll make some difference.

1 (共1页)
进入Java版参与讨论
相关主题
几个问题implements runable 和 extends thread
一个基本问题。好虫,exploit等大牛请进--关于htmlunit
关于inner class的继承weird class definition
java的接口runnableJava练习题 6
线程问题。can applet implements runnable?
一道java题Help with Tomcat for Eclipse v1.03
ZT: 关于性爱的多线程问题研究(一)Re: How to showStatus in Applet?
能这么 create thread 吗?FrameWork of Thread application 1
相关话题的讨论汇总
话题: thread话题: public话题: new话题: void话题: what