生产者-消费者模式

Posted kuillldan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生产者-消费者模式相关的知识,希望对你有一定的参考价值。

  • 生产者负责生产产品。
  • 消费者负责取走并使用产品。
  • 生产者生产完成后通知消费者可以取走产品了。
  • 消费者消费完产品后需要通知生产者生产产品。
  • 生产者没有生产完成,消费者不能取走产品。
  • 消费者没有使用完产品,生产者不能生产产品。
package main;

import java.util.*;

class TicketSeller implements Runnable
{
    public static Integer ticketSoldTotal = 0;
    private Integer totalTicket = 300;
    private Random random = new Random();
    
    @Override
    public void run()
    {
        for(int i = 0; i < 500; i++)
        {
            synchronized(this)
            {
                if(this.totalTicket > 0)
                {
                    Util.sleep(500);
                    int ticketSold = random.nextInt(5);
                    ticketSold++;
                    if((this.totalTicket - ticketSold) < 0)
                    {
                        ticketSold = this.totalTicket;
                    }
                    ticketSoldTotal += ticketSold;
                    this.totalTicket -= ticketSold;
                    System.out.println(Thread.currentThread().getName()+" sells "+ticketSold+" ticket(s), " + this.totalTicket + " left. ");
                }
            }
        }
    }
}

class Util
{
    public static void sleep(Integer sleepTime)
    {
        try
        {
            Thread.sleep(sleepTime);
        }
        catch(Exception e)
        {}
    }
}

class Resource
{
    private String name;
    public Resource(String name)
    {
        this.name = name;
    }
    
    public String getName()
    {
        return this.name;
    }
}

class WritePaper implements Runnable
{
    private Resource resourceA;
    private Resource resourceB;
    
    
    
    public WritePaper(Resource resourceA, Resource resourceB)
    {
        super();
        this.resourceA = resourceA;
        this.resourceB = resourceB;
    }



    public void run()
    {
        synchronized(this.resourceA)
        {
            Util.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "" + this.resourceA.getName());
            synchronized(resourceB)
            {
                System.out.println(Thread.currentThread().getName() + "" + this.resourceB.getName());
            }
        }
    }
}

class Product
{
    private String name;
    private String desc;
    private Boolean readyToGet = false;
    
    public Product()
    {
        super();
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getDesc()
    {
        return desc;
    }
    public void setDesc(String desc)
    {
        this.desc = desc;
    }
    
    public synchronized void setInfo(String name, String desc)
    {
        if(this.readyToGet == true)
        {
            try
            {
                super.wait();
            }
            catch(Exception e)
            {}
        }
        
        
        this.setName(name);
        Util.sleep(400);
        this.setDesc(desc);
        
        this.readyToGet = true;
        super.notifyAll();
    }
    
    public synchronized String getInfo()
    {
        if(this.readyToGet == false)
        {
            try
            {
                super.wait();
            }
            catch(Exception e)
            {}
        }
        
        String name = this.getName();
        Util.sleep(300);
        String desc = this.getDesc();
        
        this.readyToGet = false;
        super.notifyAll();
        return "Name: " + name + " Desc: " + desc;
    }
    
    @Override
    public String toString()
    {
        return "Product [name=" + name + ", desc=" + desc + "]";
    }
    
    
}

class Produce implements Runnable
{
    private Product product;
    
    public Produce(Product product)
    {
        super();
        this.product = product;
    }

    @Override
    public void run()
    {
        for(int i = 0; i < 30; i++)
        {
            if(i%2 == 0)
                this.product.setInfo("fan", "Thre gorges");
            else
                this.product.setInfo("computer", "HP");
        }
    }
}

class Comsume implements Runnable
{
    private Product product;
    
    public Comsume(Product product)
    {
        super();
        this.product = product;
    }

    @Override
    public void run()
    {
        for(int i = 0; i < 30; i++)
        {
            System.out.println(this.product.getInfo());
        }
        
    }
    
}


public class main
{
    public static void main(String[] args) throws Exception
    {
        Product product = new Product();
        Comsume c = new Comsume(product);
        Produce p = new Produce(product);
        
        List<Thread> threadList = new ArrayList<Thread>();
        threadList.add(new Thread(p));
        threadList.add(new Thread(c));
        
        for(Thread thread : threadList)
            thread.start();
        
        for(Thread thread : threadList)
            thread.join();
        
        System.out.println("///~Main done");
    }
    
    public static List<Integer> getDemoList()
    {
        Random random = new Random();
        List<Integer> list = new ArrayList<>();
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        
        return list;
    }
}

 

以上是关于生产者-消费者模式的主要内容,如果未能解决你的问题,请参考以下文章

生产者和消费者模式-代码

并行模式之生产者-消费者模式

生产消费者模式,并不是高并发模式

生产者消费者模型-Java代码实现

Java多线程:生产者消费者模型

Java并发多线程编程——生产者消费者模式示例(传统版本)