• 线程通信


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

    }

  • 相关阅读:
    29. LDAP Authentication(LDAP身份验证)
    28. Pre-Authentication Scenarios(预认证场景)
    27. Domain Object Security (ACLs)(域对象安全)
    26. Expression-Based Access Control(基于表达式的访问控制)
    24. Authorization Architecture(授权架构)
    Part V. Authorization(授权)
    23. WebSocket Security(网络套接字安全)
    springmvc中使用文件下载功能
    springmvc中使用文件上传功能
    springmvc中ModelAttribute注解应用在参数中
  • 原文地址:https://www.cnblogs.com/shenhengjia/p/9201201.html
Copyright © 2020-2023  润新知