请问以下代码有何问题如何解决
public class TestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource r=new Resource();
Input in=new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
//描述资源
class Resource{
String name;
String sex;
}
//赋值线程任务
class Input implements Runnable{
private Resource r;
Input(Resource r){//任务一初始化就必须有要处理的资源。
this.r=r;
}
public void run(){
int x=0;
while(true){
if(x==0){
r.name="张飞";
r.sex="男";
}else{
r.name="rose";
r.sex="name";
}
x=(x+1)%2;//实现切换
}
}
}
//获取值线程任务
class Output implements Runnable{
private Resource r;
Output(Resource r){
this.r=r;
}
public void run(){
while(true){
System.out.println(r.name+"....."+r.sex);
}
}
}
|
|