【牛客带你学编程Java方向】项目练习第6期(截止5.2)

    
Java项目练习:第6期
练习时间:4月16日-5月2日(2周,劳动节顺延)
活动规则:
  • 每一期一个项目,届时会开新帖发布
  • 学员直接将答案提交到该贴评论区即可
  • 两周后,公布导师参考答案
  • 导师评选出当期最佳代码(将被设置为精彩回复

奖励:牛客大礼包一份(牛客定制水杯 牛客定制笔 牛客定制程序员徽章 滑稽抱枕)
参与方式:直接将你的代码回复到本帖评论区

-----------------------------------------------------

本期题目:

实现生产者消费者模型(60分钟)

需求描述:

在一个系统中,存在生产者和消费者两种角色,他们通过内存缓冲区进行通信,生产者生产消费者需要的资料,消费者把资料做成产品,先要求模拟实现一个生产者消费者模型,具体要求如下: 

  1. 生产者与消费者线程独立,通过不同线程实现
  2. 内存缓冲区为空的时候消费者必须等待,而内存缓冲区满的时候,生产者必须等待
  3. 多线程对临界区资源的操作时候必须保证在读写中只能存在一个线程

考查知识点:

  • Java多线程的概念及其基本使用方法
  • 并发同步与互斥基础
  • 集合类与并发集合的高级特性

参考知识点:《java基础入门》第5章、第7章

参与方式:直接将你的代码回复到本帖评论区

全部评论
import java.util.*; public class Test6 {     public static void main(String[] args){         Buffer bu=new Buffer();         Production pro=new Production(bu);         Consumption con=new Consumption(bu);         new Thread(pro).start();         new Thread(con).start();     } } //自定义临界区 class Buffer{     //数据缓冲区     private LinkedList<Integer> list=new LinkedList<Integer>();     //设定缓冲区最大容量     private final int max=20;     //生产者方法     public synchronized void Production(Integer product){         try{             while(list.size()>=max){                 this.wait();             }             list.add(product);             System.out.println("生产信息:"+product+"完成");             this.notify();         }catch(Exception e){             e.printStackTrace();         }     }     //消费者方法     public synchronized void Consumption(){         try{             while(list.size()<=0){                 this.wait();             }             Integer consume=list.removeFirst();             System.out.println("产品"+consume+"已被消费");             this.notify();         }catch(Exception e){             e.printStackTrace();         }     } } //自定义生产者线程 class Production implements Runnable{     private Integer product=0;     private Buffer bu;     public Production(Buffer bu){         this.bu=bu;     }     public void run(){         while(true){             bu.Production(product++);         }     } } //自定义消费者线程 class Consumption implements Runnable{     private Buffer bu;     public Consumption(Buffer bu){         this.bu=bu;     }     public void run(){         while(true){             bu.Consumption();         }     } }
点赞 回复
分享
发布于 2018-04-18 11:40
package programingModel; /*  * 生产者与消费者模型  * 多线程的使用  */ //主函数类 public class ProductorAndConsumer {  public static void main(String[] args)  {   //定义存储数据的仓库   Storage storage=new Storage(100,0);      //生产者   Productor p1=new Productor(10,storage);   Productor p2=new Productor(50,storage);   Productor p3=new Productor(70,storage);   Productor p4=new Productor(20,storage);   Productor p5=new Productor(60,storage);   Productor p6=new Productor(40,storage);   Thread tp1=new Thread(p1);   Thread tp2=new Thread(p2);   Thread tp3=new Thread(p3);   Thread tp4=new Thread(p4);   Thread tp5=new Thread(p5);   Thread tp6=new Thread(p6);   //消费者   Consumer c1=new Consumer(25,storage);   Consumer c2=new Consumer(60,storage);   Consumer c3=new Consumer(40,storage);   Consumer c4=new Consumer(10,storage);   Consumer c5=new Consumer(50,storage);   Consumer c6=new Consumer(20,storage);   Consumer c7=new Consumer(15,storage);   Consumer c8=new Consumer(30,storage);   Thread tc1=new Thread(c1);   Thread tc2=new Thread(c2);   Thread tc3=new Thread(c3);   Thread tc4=new Thread(c4);   Thread tc5=new Thread(c5);   Thread tc6=new Thread(c6);   Thread tc7=new Thread(c7);   Thread tc8=new Thread(c8);      //开始进程   tp1.start();   tp2.start();   tp3.start();   tp4.start();   tp5.start();   tp6.start();   tc1.start();   tc2.start();   tc3.start();   tc4.start();   tc5.start();   tc6.start();   tc7.start();   tc8.start();  } } //存储模型 class Storage {  private int maxStorage;   //定义最大存储量  private int currentStorage;  //定义已使用的存储量    public Storage(int max,int cur)  {   maxStorage=max;   currentStorage=cur;  }  public int getMaxStorage() {   return maxStorage;  }  public void setMaxStorage(int maxStorage) {   this.maxStorage = maxStorage;  }  public int getCurrentStorage() {   return currentStorage;  }  public void setCurrentStorage(int currentStorage) {   this.currentStorage = currentStorage;  }  //接收来自生产者生产的产品  public boolean inProduct(int nProduct)  {   if(currentStorage+nProduct>maxStorage)   {    System.out.println("仓库已满!");    return false;   }   else   {    currentStorage+=nProduct;    System.out.println("仓库产品+"+nProduct+"  "+"仓库剩余产品:"+currentStorage);    return true;   }   /*   synchronized(currentStorage)   {    while(currentStorage+nProduct>maxStorage)    {     try {      currentStorage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    currentStorage+=nProduct;    System.out.println("产品已进入仓库 数量:"+nProduct+"仓库已有数量"+getCurrentStorage());    currentStorage.notify();   }   */  }  //向消费者提***品  public boolean outProduct(int nConsume)  {   if(currentStorage<nConsume)   {    System.out.println("仓库数量不足!");    return false;   }   else   {    currentStorage-=nConsume;    System.out.println("仓库产品-"+nConsume+"  "+"仓库剩余产品:"+currentStorage);    return true;   }   /*   synchronized(currentStorage)   {    while(currentStorage<nConsume)    {     try {      currentStorage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    currentStorage-=nConsume;    System.out.println("已从仓库中取出 数量:"+nConsume+"仓库剩余数量"+getCurrentStorage());    currentStorage.notify();   }*/  } } //生产者模型 class Productor implements Runnable {  private int produce;  //生产产品数量  private Storage storage; //生产产品存储到哪个仓库    public Productor(int produce,Storage storage)  {   this.produce=produce;   this.storage=storage;  }  //get set方法  public int getProduction() {   return produce;  }  public void setProduction(int production) {   this.produce = production;  }  public Storage getStorage() {   return storage;  }  public void setStorage(Storage storage) {   this.storage = storage;  }  //生产者生产  public boolean product()  {   return storage.inProduct(produce);  }  //执行线程时所做处理  @Override  public void run() {   synchronized(storage)   {    while(!product())    {     try {      storage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    storage.notifyAll();   }   /*   synchronized(storage)   {    while(!product())    {     System.out.println("产品未进入仓库 数量:"+produce+"仓库空间"+(storage.getMaxStorage()-storage.getCurrentStorage()));     try {      this.wait();     } catch (InterruptedException e) {     }    }    System.out.println("产品已进入仓库 数量:"+produce+"仓库已有数量"+storage.getCurrentStorage());    this.notifyAll();   }   */  /* product();*/  } } //消费者模型 class Consumer implements Runnable {  private int need;   //消费产品数量  private Storage storage; //获取产品数量的仓库    public Consumer(int need,Storage storage)  {   this.need=need;   this.storage=storage;  }  //get set 方法  public int getNeed() {   return need;  }  public void setNeed(int need) {   this.need = need;  }  public Storage getStorage() {   return storage;  }  public void setStorage(Storage storage) {   this.storage = storage;  }    //消费者消耗  public boolean consume()  {   return storage.outProduct(need);  }  @Override  public void run()  {   synchronized(storage)   {    while(!consume())    {     try {      storage.wait();     } catch (InterruptedException e) {      e.printStackTrace();     }    }    storage.notifyAll();   }   /*   synchronized(storage)   {    while(!consume())    {     System.out.println("未能从仓库取出  数量:"+need+"仓库存在数量"+storage.getCurrentStorage());     try {      this.wait();     } catch (InterruptedException e) {     }    }    System.out.println("已从仓库中取出 数量:"+need+"仓库剩余数量"+storage.getCurrentStorage());    this.notifyAll();   }*/   /*consume();*/  } }
点赞 回复
分享
发布于 2018-04-18 20:31
博乐游戏
校招火热招聘中
官网直投
package com.nowcode.www.demo; import java.util.concurrent.BlockingQueue; public class Producer implements Runnable{     BlockingQueue<String> queue ;     /**      * @param queue      */     public Producer(BlockingQueue<String> queue) {         super();         this.queue = queue;     }     @Override     public void run() {         try {             System.out.println("i hava made a product" + Thread.currentThread().getName());             String temp = "consumer " + Thread.currentThread().getName();             queue.put(temp);//如果队列满了就会阻塞         } catch (Exception e) {             e.printStackTrace();         }     }      } package com.nowcode.www.demo; import java.util.concurrent.BlockingQueue; public class Consumer implements Runnable{     BlockingQueue<String> queue;          /**      * @param queue      */     public Consumer(BlockingQueue<String> queue) {         super();         this.queue = queue;     }     @Override     public void run() {         try {             String temp = queue.take();//如果为空会阻塞             System.out.println(temp);         } catch (Exception e) {             e.printStackTrace();         }      } } package com.nowcode.www.demo; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class Main {     public static void main(String[] args) {         BlockingQueue<String> queue = new LinkedBlockingQueue<String>(2); //设置2 ,不设置的话默认值为MAX_VALUE         Producer p = new Producer(queue);         Consumer c = new Consumer(queue);         for (int i = 0; i < 10; i++) {             new Thread(p,"produce" + i).start();             new Thread(c, "consumer" + i).start();         }     } }
点赞 回复
分享
发布于 2018-04-24 22:28

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务