java 消费与生产者的简单编写案例

自己定义的案例:

由四个类组成:

生产者类:

package xiancheng2;

public class productor_customer implements Runnable{

       private share ziyuan=null;

       public productor_customer(share ziyuan) {

        // TODO Auto-generated constructor stub

           this.ziyuan=ziyuan;

    }

       public void run()

       {

           for(int i=0;i<50;i++)

           {

               if(i%2==0)

               {

               ziyuan.set("xiaoming",sex.female);

               }

               else

               {

                   ziyuan.set("xiaowang",sex.male);

               }

           }

       }

}

消费者类:

package xiancheng2;

public class customer implements  Runnable {

    private share ziyuan=null;

    public customer(share ziyuan) {

        // TODO Auto-generated constructor stub

        this.ziyuan=ziyuan;

    }

    public void run() {

        for(int i=0;i<50;i++)

        {

            

            ziyuan.print();;

        }

    }

      

}

共享资源类:

package xiancheng2;

enum sex {

    male, female;

}

public class share {

    private String name;

    private sex sexy;

    private boolean isempty=true;

    synchronized public void set(String n, sex s) {

        

        try {

            while(!isempty)

            {

                this.wait();

            }

            

            this.name = n;

            Thread.sleep(10);

            this.sexy = s;

            isempty=false;

            this.notify();

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        

    }

    synchronized public void print() {

        try {

            Thread.sleep(10);

            while(isempty)

            {

                this.wait();

            }

            if(this.name!=null)

            System.out.println("消费者是" + this.name + "性别为" + this.sexy);

            isempty=true;

            this.notify();

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        

    }

}

主类:

package xiancheng2;

public class yunxing {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

             share pShare=new share();

             Thread qThread=new Thread(new productor_customer(pShare));

             Thread thread=new Thread(new customer(pShare));

             qThread.start();

             thread.start();

    }

}

package xiancheng2;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class tongbusuo {

    private String name;

    private sex sexy;

    private boolean isempty=true;

    private final Lock lock=new ReentrantLock();

    private Condition condition=lock.newCondition();    

        

        

    

     public void set(String n, sex s) {

        lock.lock();

        

        try {

            

            while(!isempty)

            {

                condition.await();

            }

            this.name = n;

            Thread.sleep(10);

            this.sexy = s;

            isempty=false;

            condition.signal();

            

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        finally {

            lock.unlock();

        }

    }

public void print() {

        lock.lock();

        try {

            while(isempty)

            {

                condition.await();

            }

            Thread.sleep(10);

            if(this.name!=null)

            System.out.println("消费者是" + this.name + "性别为" + this.sexy);

            isempty=true;

            condition.signal();

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        finally {

            lock.unlock();

        }

    }

}

猜你喜欢

转载自blog.csdn.net/zhouzhou_98/article/details/81304842