zoukankan      html  css  js  c++  java
  • 线程通信

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;

    class person
    {
      private String name;
      private String xingbie;
      boolean panduan=false;
      int y=0;
      Lock l=new ReentrantLock();//创建锁。
      Condition c1=l.newCondition();
      Condition c2=l.newCondition();
      public String getName()

      {
        return name;
      }
      public void setName(String name)

       {
        this.name = name;
      }
      public String getXingbie()

       {
        return xingbie;
      }
      public void setXingbie(String xingbie)

      {
        this.xingbie = xingbie;
      }


      public void fangru() throws InterruptedException
      {
        l.lock();
        try

        {

          while(panduan)

          {
            c1.await();
          }
          if(y==0)
          {
            setName("张三");
            setXingbie("男");
          }
        else
        {
          setName("李四");
          setXingbie("女");
        }
        y=(y+1)%2;
        panduan=true;
        c2.signal();
        }
        finally
        {
          l.unlock();
        }

      }

      public void quchu() throws InterruptedException
      {
        l.lock();
        try{
          while(!panduan)
          {
            c2.await();
          }
          System.out.println(this.getName()+"----"+this.getXingbie());
          panduan=false;
          c1.signal();
         }
        finally
        {
          l.unlock();
        }

      }

    }

    class du implements Runnable
    {
      person p;
      du(person p)
      {
        this.p=p;
      }

      public void run()

      {
        while(true)
        {
          try

           {
            p.fangru();
          } catch (InterruptedException e)

           {
            e.printStackTrace();
          }
        }
      }
    }
    class qu implements Runnable
    {
      person p;
      qu(person p)
      {
        this.p=p;
      }
      public void run()

      {
        while(true)
        {
          try

           {
            p.quchu();
          }

           catch (InterruptedException e)

           {
            e.printStackTrace();
          }
        }
      }
    }

    public class xianchengtongxin

    {

      public static void main(String[] args)

       {

        person p=new person();
        du du=new du(p);
        qu qu=new qu(p);
        Thread t1=new Thread(du);
        Thread t2=new Thread(qu);
        Thread t3=new Thread(du);
        Thread t4=new Thread(qu);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
      }

    }

  • 相关阅读:
    [音乐欣赏]为你读诗背景音乐,音乐电台
    [读书笔记]1368个单词就够了
    [读书笔记]项目管理实战:Microsoft Project精髓与方法
    [音乐欣赏]动力火车 艾琳娜
    [办公自动化]PDF大小不一如何调整
    [读书笔记]左手数据,右手图表
    [写作新思路]数据分析
    揪出Android流氓软件
    [办公自动化]Wlan无法启动,无法连接无线网wifi,所有无线网都搜索不到
    日常UVA题目英语积累
  • 原文地址:https://www.cnblogs.com/shenhengjia/p/9201201.html
Copyright © 2011-2022 走看看