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();
      }

    }

  • 相关阅读:
    env文件的作用
    Ubuntu 卸载wine
    Linux配置yaf3.x.x环境
    yaf中使用Cli模式,也就是定时器
    Yaf 在Bootstrap中注册变量,然后在其他地方进行使用!
    yaf 查看配置
    yaf配置通用函数
    一个严谨的接口调用
    后台基础表
    tensorflow环境搭建
  • 原文地址:https://www.cnblogs.com/shenhengjia/p/9201201.html
Copyright © 2011-2022 走看看