美文网首页Java
Java16-7 练习

Java16-7 练习

作者: 第二套广播体操 | 来源:发表于2018-11-26 21:31 被阅读0次
    class Data{
        String name;
        String sex;
    }
    class Input implements Runnable{
        private Data data;
    
        public Input(Data data) {
            this.data = data;
        }
    
        @Override
        public void run() {
              int x=0;
            for (int i = 0; i <20; i++) {
    
    
              if (x==0){
                 data.name="于松江";
                 data.sex="男";
              }
              else {
                  data.name="程泽琪";
                  data.sex="女";
              }
               x=(x+1)%2;
            }
        }
    }
    class OutPut implements Runnable{
        private Data data;
    
        public OutPut(Data data) {
            this.data = data;
        }
     @Override
        public void run() {
            for (int i = 0; i <20; i++) {
                System.out.println("线程"+Thread.currentThread().getName()+"\t"+data.name+data.sex);
            }
        }
    }
    
    public class InputDemo {
        public static void main(String[] args)
        {
            Data data=new Data();
            Input input=new Input(data);
            OutPut outPut=new OutPut(data);
            Thread thread1=new Thread(input,"录入线程");
            Thread thread2=new Thread(outPut,"输出线程");
        }
    }
    
    
    程序结果

    解决多线程中输出错误的问题

    分析:
    1、找到共享数据
    2、线程任务中是否有多条操作共享数据的代码
    解决方法:

    class Data{
       private String name;
        private String sex;
        public synchronized void set(String name,String sex){
            this.name=name;
            this.sex=sex;
        }
        public synchronized void out(){
            System.out.println("线程"+Thread.currentThread().getName()+"\t"+name+"\t"+sex);
        }
    }
    

    同步 但是会出现一出来一大片的情况 所以我们使用等待唤醒机制改善

    class Data{
       private String name;
        private String sex;
        private boolean sign;//定义标记
        public synchronized void set(String name,String sex){
            if (sign)
                try {this.wait();}catch (InterruptedException e){}
           this.name=name;
            this.sex=sex;
            sign=true;
            this.notify();
    
        }
    

    使用Lock和监听器

    class Data{
       private String name;
        private String sex;
        private boolean sign;//定义标记
        private final Lock lock=new ReentrantLock();//锁
        private Condition in =lock.newCondition();//录入监视器
        private Condition out=lock.newCondition();//输出监视器
        public void set(String name,String sex){
            lock.lock();
            while (sign)
                try {
                    in.await();}catch (InterruptedException e){}
           this.name=name;
            this.sex=sex;
            sign=true;
            out.signal();
            lock.unlock();
    
        }
        public synchronized void out(){
            lock.lock();
             if (!sign)
                 try {
                     out.await();}catch (InterruptedException e){}
            System.out.println("线程"+Thread.currentThread().getName()+"\t"+name+"\t"+sex);
            sign=false;
           in.signal();
           lock.unlock();
    
        }
    }
    

    相关文章

      网友评论

        本文标题:Java16-7 练习

        本文链接:https://www.haomeiwen.com/subject/uqyiqqtx.html