本帖最后由 as604049322 于 2014-12-26 10:43 编辑
难得得到老师的认可:lol,今天起床又稍微作了一点改进。细节问题还望王老师指导一下。
运行效果:
代码如下:
- import java.util.concurrent.locks.*;
- class ProducerConsumer4
- {
- public static void main(String[] args)
- {
- Resource r=new Resource();
- Producer p1=new Producer("超级生产机器人1",r);
- Producer p2=new Producer("超级生产机器人2",r);
- Producer p3=new Producer("超级生产机器人3",r);
- Producer p4=new Producer("超级生产机器人4",r);
- Producer p5=new Producer("超级生产机器人5",r);
- Producer p6=new Producer("超级生产机器人6",r);
- Consumer c=new Consumer(r);
- p4.work();
- p5.work();
- p6.work();
- new Thread(c,"超级商店1").start();
- new Thread(c,"超级商店2").start();
- new Thread(c,"超级商店3").start();
- new Thread(c,"超级商店4").start();
- int count=0;
- while(true){
- p4.over();
- p5.over();
- p6.over();
- p1.work();
- p2.work();
- p3.work();
- try{Thread.sleep(1000);}catch(Exception e){}
- p1.over();
- p2.over();
- p3.over();
- p4.work();
- p5.work();
- p6.work();
- count=(count+1)%10;
- if(count==5){
- p1.over();
- p2.over();
- p3.over();
- p4.over();
- p5.over();
- p6.over();
- System.out.println("<-----所有机器人集体休息20秒------>");
- try{Thread.sleep(20000);}catch(Exception e){}
- }
- }
- }
- }
- class commodity
- {
- String name;//产品名称
- int number;//产品编号
- commodity(int num){
- this.name=creatRandomName();
- this.number=num;
- }
- public static String creatRandomName(){
- String[] name={"茶杯","茅台酒","21金维他","两面针","牙膏","电视机","计算机","桌子","衣服"};
- return name[(int)(Math.random()*name.length)];
- }
- }
- class Resource
- {
- final commodity[] storehouse = new commodity[100];//仓库中的保存的货物
- int total;//仓库中当前的货物总数
- int proPointer;//生产者当前指针
- int conPointer;//消费者当前指针
-
- int count;//产品编号
- private Lock lock=new ReentrantLock();
- private Condition pro=lock.newCondition();
- private Condition con=lock.newCondition();
- public void produce() throws InterruptedException {
- lock.lock();
- try{
- while(total==storehouse.length)
- pro.await();
- storehouse[proPointer]=new commodity(count++);
- System.out.println(Thread.currentThread().getName()+":生产一个商品,信息如下:");
- System.out.println("产品名称:"+storehouse[proPointer].name+";产品编号:"+storehouse[proPointer].number);
- System.out.println("-----------------------------------------------------");
- proPointer=(proPointer+1)%storehouse.length;
- ++total;
- con.signal();
- }finally{
- lock.unlock();
- }
- }
- public void consume() throws InterruptedException {
- lock.lock();
- try{
- while(total==0)
- con.await();
- commodity temp=storehouse[conPointer];
- conPointer=(conPointer+1)%storehouse.length;
- --total;
- System.out.println(Thread.currentThread().getName()+":某个商品被购买,信息如下:");
- System.out.println("产品名称:"+temp.name+";产品编号为:"+temp.number);
- System.out.println("购买人信息:");
- System.out.println("姓名:"+person.getName()+";性别:"+person.getSex());
- System.out.println("手机号:"+person.getCellphoneNumber());
- System.out.println("-----------------------------------------------------");
- pro.signal();
- }finally{
- lock.unlock();
- }
- }
- }
- class person
- {
- public static final String[] firstName={"王","李","张","刘", "陈","杨","黄","赵", "周","吴","徐","孙", "马","胡","朱","郭", "何","罗","高","林",
- "郑","梁","谢","唐", "许","冯","宋","韩", "邓","彭","曹","曾", "田","于","肖","潘", "袁","董","叶","杜",
- "丁","蒋","程","余", "吕","魏","蔡","苏", "任","卢","沈","姜", "姚","钟","崔","陆", "谭","汪","石","付",
- "贾","范","金","方", "韦","夏","廖","侯", "白","孟","邹","秦", "尹","江","熊","薛", "邱","闫","段","雷",
- "季","史","陶","毛", "贺","龙","万","顾", "关","郝","孔","向", "龚","邵","钱","武", "扬","黎","汤","戴",
- "严","文","常","牛", "莫","洪","米","康", "温","代","赖","施"};
- public static final String[] lastName={"振","旦","云","祥","哲","欣","华","彦","淇","华","梓","刚","洋","煜","霖","键"
- "利","培","欣","宇","俊","泊","政","文","杰","海","泽","泊","鑫","晖","子","硕","霖","鑫","培",
- "智","生","明","鑫","子","哲","键","强","刚","键","子","杰","杰","成","铠","兰","霖","霖","灏"
- "泉","泉","新","锋","瑞","翔","纯","鸿","益","睿","厚","晖","超","曦","智","博","智","杰","彦",
- "翔","轩","泉","文","慧","瑞","祺","君","明","思","煜","铠","欣","梓","智","嵘","欣","洁","程",
- "昌","咏","明","宝","祺","函","龙","宇","明","存","政","奕","梓","鑫","柔","方","霖","朕","嵘",
- "松","鑫","咏","欣","睿","铭","海","霖","彦","泽","霖","宇","翌","子","政","明","佳","铠","明",
- "富","明","柔","程","彦","汪","英","鑫","祺","宇","杰","汪","刚","金","桂","晏","嵘","宝","正",
- "子","超","梓","洋","欣","晖","凯","兰","洋","文","霖","继","昌","君","彤","欣","华","奕","圩"
- "文","晏","君","泽","柔","利","函","文","星","正","功","茂","雪","明","成","松","瑞","厚","宇",
- "杰","泽","晔","嘉","泽","阳","俊","堇","存","铭","思","宇","杰","希","鑫","键","明","希","韬",
- "欣","智","思","鑫","阳","鑫","彦","华","铠","豪","成","明","文","旻","嘉","旻","政","新","泰",
- "存","荣","益","金","锋","鑫","俊","振","永","滨","君","智","博","杰","晔","子","文","瑞","晖",
- "振","君","程","祺","炎","睿","政","晔","锦","佳","宇"};
- public static String getName(){
- String name=firstName[(int)(Math.random()*112)]+lastName[(int)(Math.random()*lastName.length)];
- if((int)(Math.random()*2)==0)
- name+=lastName[(int)(Math.random()*lastName.length)];
- if((int)(Math.random()*20)==5)
- name+=lastName[(int)(Math.random()*lastName.length)];
- return name;
- }
- public static String getSex(){
- if((int)(Math.random()*2)==1)
- return "男";
- else
- return "女";
- }
- public static String getCellphoneNumber(){
- String[] firstNumber={"134","135"," 136","137","138","139","150","151","152","157","158","159","187","188",//移到
- "130","131","132","155","156","185","186",//联通
- "133","153","180","189"};//电信
- return firstNumber[(int)(Math.random()*firstNumber.length)]+(int)(Math.random()*9000+1000)+(int)(Math.random()*9000+1000);
- }
- }
- class Producer
- {
- private final Resource r;
- private boolean flag;
- private final String name;
- private Thread t;
- Producer(String name,Resource r){
- this.name=name;
- this.r=r;
- }
- public void work(){
- flag=true;
- t=new Thread(name){
- public void run(){
- while(flag){
- try{
- r.produce();
- }catch (InterruptedException e){
- System.out.println(Thread.currentThread().getName()+":发生中断异常");
- }
- }
- System.out.println(Thread.currentThread().getName()+":我休息1秒钟");
- System.out.println("-----------------------------------------------------");
- }
- };
- t.setPriority(Thread.MIN_PRIORITY);
- t.start();
- }
- public void over(){
- flag=false;
- t.interrupt();
- }
- }
- class Consumer implements Runnable
- {
- private final Resource r;
- Consumer(Resource r){
- this.r=r;
- }
- public void run(){
- while(true){
- try{
- r.consume();
- }catch (InterruptedException e){
- e.toString();
- }
- }
- }
- }
复制代码
|
-
1.png
(11.83 KB, 下载次数: 3)
-
2.png
(11.68 KB, 下载次数: 2)
-
1.png
(11.83 KB, 下载次数: 2)
-
2.png
(11.68 KB, 下载次数: 1)
组图打开中,请稍候......
|