当前位置: 技术问答>java相关
thread考题一道??,为何编译不通过,
来源: 互联网 发布时间:2015-03-13
本文导语: public class x implements Runnable{ private int x; private int y; public static void main(String args[] ){ x that = new x(); (new Thread(that)).start(); (new Thread(that)).start(); } public synchronized void run(){ for( ; ; ){ x++; y++;...
public class x implements Runnable{
private int x;
private int y;
public static void main(String args[] ){
x that = new x();
(new Thread(that)).start();
(new Thread(that)).start();
}
public synchronized void run(){
for( ; ; ){
x++;
y++;
System.out.Println("x=" + x + ", y=" +y);
}
}
}
private int x;
private int y;
public static void main(String args[] ){
x that = new x();
(new Thread(that)).start();
(new Thread(that)).start();
}
public synchronized void run(){
for( ; ; ){
x++;
y++;
System.out.Println("x=" + x + ", y=" +y);
}
}
}
|
好多x啊,编译器也搞不清谁是谁了。
1、System.out.Println(... -> System.out.println(...
2、把文件名入类名改成别的(如Myx)
参照JDK API文档 -> Runnable
试试如下代码
public static void main(String args[] ){
Myx that = new Myx();
(new Thread(that)).start();
Myx that2 = new Myx();
(new Thread(that2)).start();
}
1、System.out.Println(... -> System.out.println(...
2、把文件名入类名改成别的(如Myx)
参照JDK API文档 -> Runnable
试试如下代码
public static void main(String args[] ){
Myx that = new Myx();
(new Thread(that)).start();
Myx that2 = new Myx();
(new Thread(that2)).start();
}
|
//我是想知道为何x,y相等。
注意:〉〉〉synchronized void run(){
你的run方法是同步的啊!
注意:〉〉〉synchronized void run(){
你的run方法是同步的啊!
|
System.out.Println(... -> System.out.println(...
大家都说了
x that = new x();//有没有类x?如果没有添加一个
这样在我的机器上编译通过了
大家都说了
x that = new x();//有没有类x?如果没有添加一个
这样在我的机器上编译通过了