A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zhangshuai_123 中级黑马   /  2015-7-1 15:03  /  689 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

请问以下代码有何问题如何解决
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);
                }
        }
}

4 个回复

倒序浏览
两个线程同时操作 Resource 实例,导致姓名和性别混乱,可以加入synchronized.
  1. public class TestDemo {

  2.         public static void main(String[] args) {
  3.                 // TODO Auto-generated method stub
  4.                 Resource r=new Resource();
  5.                 Input in=new Input(r);
  6.                 Output out=new Output(r);
  7.                 Thread t1=new Thread(in);
  8.                 Thread t2=new Thread(out);
  9.                 t1.start();
  10.                 t2.start();
  11.         }

  12. }
  13. //描述资源
  14. class Resource{
  15.         String name;
  16.         String sex;
  17.         public synchronized int set ( int x )
  18.         {
  19.                
  20.                 if(x==0)
  21.                 {
  22.                         this.name="张飞";
  23.                         this.sex="男";
  24.                         }
  25.                 else
  26.                 {
  27.                         this.name="rose";
  28.                         this.sex="name";
  29.                         }
  30.                 x=(x+1)%2;//实现切换
  31.                 return x;
  32.         }
  33.         public synchronized void print ()
  34.         {
  35.                 System.out.println(this.name+"....."+this.sex);
  36.         }
  37. }
  38. //赋值线程任务
  39. class Input implements Runnable{
  40.         private Resource r;
  41.         Input(Resource r){//任务一初始化就必须有要处理的资源。
  42.         this.r=r;        
  43.         }
  44.         public void run(){
  45.                 int x = 0;
  46.             while(true)
  47.                        
  48.                 {
  49.                         x=r.set(x);
  50.    
  51.                 }

  52.         }
  53. }
  54. //获取值线程任务
  55. class Output implements Runnable{
  56.         private Resource r;
  57.         Output(Resource r){
  58.                 this.r=r;
  59.         }
  60.         public void run(){
  61.                 while(true)
  62.                         r.print();
  63.       
  64.                
  65.         }
  66. }
复制代码
回复 使用道具 举报
还没有学到     帮不上忙了
回复 使用道具 举报
兄弟加油!!
回复 使用道具 举报
哇,好厉害的样子,不过我也看不懂这代码,希望楼主早点找到答案。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马