• java例程练习(多线程综合练习[生产者消费者问题])


    /*
     * 生产者与消费者问题
     * 此问题用到Object类的wait(),notify()
     * 还要注意一个问题:
     * sleep()与wait()的区别:
     * 		sleep()过程中,对象仍未解锁
     * 		wait ()过程中,对象解锁,其他线程可以访问当前对象,当唤醒时需重新锁定
     */
    public class Test {
    	public static void main(String[] args) {
    		ThingsStack ts = new ThingsStack();
    		Producer p = new Producer(ts);
    		Consumer c = new Consumer(ts);
    		new Thread(p).start();
    		new Thread(p).start();
    		new Thread(p).start();
    		
    		new Thread(c).start();
    		new Thread(c).start();
    		new Thread(c).start();
    	}
    }
    
    class Things {
    	int id;
    	Things(int id) {
    		this.id = id;
    	}
    	
    	public String toString() {
    		return "Things :" + id;
    	}
    }
    
    class ThingsStack {
    	int index = 0;
    	Things [] arrThings = new Things[6];
    	
    	public synchronized void push(Things t) {
    		
    		//if被打断后会跳出继续执行,用while语句更安全
    //		if(index == arrThings.length) {
    //			try {
    //				this.wait();//Object类的wait();
    //			} catch(InterruptedException e) {
    //				e.printStackTrace();
    //			}	
    //		}
    		
    		while(index == arrThings.length) {
    			try {
    				this.wait();//Object类的wait();
    			} catch(InterruptedException e) {
    				e.printStackTrace();
    			}	
    		}
    		
    		//this.notify();
    		this.notifyAll();
    		arrThings[index] = t;
    		index ++;
    	}
    	
    	public synchronized Things pop() {
    		//if被打断后会跳出继续执行,用while语句更安全
    //		if(index == 0) {
    //			try {
    //				this.wait();//Object类的wait();
    //			} catch(InterruptedException e) {
    //				e.printStackTrace();
    //			}	
    //		}
    		
    		while(index == 0) {
    			try {
    				this.wait();//Object类的wait();
    			} catch(InterruptedException e) {
    				e.printStackTrace();
    			}	
    		}
    		
    		//this.notify();
    		this.notifyAll();
    		
    		index --;
    		return arrThings[index];
    	}
    }
    
    class Producer implements Runnable {
    	ThingsStack ts = null;
    	
    	Producer(ThingsStack ts) {
    		this.ts = ts;
    	}
    	
    	public void run() {
    		for(int i = 0; i < 20; i++) {
    			Things t = new Things(i);
    			ts.push(t);
    			
    System.out.println("正在生产:" + t);	
    
    			try {
    				Thread.sleep((int)Math.random() * 1000);
    			} catch(InterruptedException e) {
    				e.printStackTrace();
    			}
    			
    		}
    	}
    }
    
    class Consumer implements Runnable {
    	ThingsStack ts = null;
    	
    	Consumer(ThingsStack ts) {
    		this.ts = ts;
    	}
    	
    	public void run() {
    		for(int i = 0; i < 20; i++) {
    			Things t = ts.pop();
    			
    System.out.println("正在消费:" + t);
    			
    			try {
    				Thread.sleep((int)Math.random() * 1000);
    			} catch(InterruptedException e) {
    				e.printStackTrace();
    			}
    			
    		}
    	}
    }
    

  • 相关阅读:
    Linux运维之监控CPU和内存的日志工具
    Linux磁盘缓存的有趣实验
    Linux运维之内存分析2
    Linux运维之内存分析
    使用kubectl create 和 kubectl apply创建资源对象的区别
    Docker学习:Image的本地存储结构
    Docker 空间使用分析与清理
    HeidiSQL、Navicat、mysql命令和source命令导入sql脚本的速度比较
    Centos 7.2天兔(Lepus 3.8)数据库监控系统部署
    MegaCli 监控raid状态
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671688.html
Copyright © 2020-2023  润新知