本帖最后由 烟花雨 于 2013-10-28 12:23 编辑
输出结果为什么只有mike........nannannan,而没有丽丽.......女女女女呢?
public class Tongxi {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person a=new Person();
Input in=new Input(a);
Output out=new Output(a);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
class Person{
String name;
String sex;
}
class Input implements Runnable{
Person r;
public Input(Person r){
this.r=r;
}
public void run(){
while(true){
int x=0;
if(x==0){
r.name="mike";
r.sex="nannannan";
}
else{
r.name="丽丽";
r.sex="女女女女女";
}
x=(x+1)%2;
System.out.println(x);
}
}
}
class Output implements Runnable{
Person r;
public Output(Person r){
this.r=r;
}
public void run(){
try{Thread.sleep(10);}catch(Exception e){e.printStackTrace();}
while(true){
System.out.println(r.name+"........"+r.sex);
}
}
}
|