线程练习——生产者消费者

package com.cxl.thread.produce;
/**
 * 仓库
 * @author Administrator
 *
 */
public class Storage {

	private String name;
	private String color;
	private boolean isProduce;
	/**
	 * 生产者生产产品
	 * @param name
	 * @param color
	 */
	public synchronized void put(String name, String color) {
		while(isProduce){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.name=name;
		this.color=color;
		System.out.println("生产者生产了一个"+color+"的"+name);
		isProduce=true;
		notify();
		
	}
/**
 * System.out.println与System.err.println的区别 
1、System.out.println能重定向到别的输出流,在屏幕上将看不到打印的东西,而System.err.println只能在屏幕上实现
打印,即使你重定向了也一样。例如:System.setOut(new   PrintStream(new   FileOutputStream(new   File( "c
:/test.txt "))));System.out.println( "haha ");
2、当向控制台输出信息时,开发者有两个选择:System.out和System.err。使用者更倾向于输出的是System.out,而如果是 System.err则输出“error”。尽管这看起来是显而易见的,但很多开发者都不了解为什么出错和调试时使用System.err。(如果 你使用err打印出的 字符串,在eclipse的console会显示成红色的哦。)当输出一个流时,JVM和操作系统共同决定何时输出这个流。也就是说,尽管开发者键入了:  System.out.print("TestOutput:");JVM和操作系统的组合体并不会立即输出这个流。相反,它将保持等待状态直到将要输出的东西达到一定的量。  
假设输入以下指令:System.out.println("Debugging   Info.");JVM可能同意输出;然而,操作系统可能决定暂不输出。由于这个原因,在调试程序时想要发现出错的位置就有可能成为问题。考虑以下的程序:     
  for(int   i=0;   i<56;   i++)   {  
  System.out.println(i);  
  ...   //   containing   an   error  
  }  
  错误可能出现在i等于54时,但是可能JVM在i等于49时就结束输出了。50到54仍然存在于缓存中,结果也就丢失了。使用System.err来报告错误、调试程序就可以避免这种情况出现,它将使每一次操作的结果都输出出来。例如以下程序:    
  for(int   i=0;   i<56;   i++)   {  
  System.err.println(i);  
  ...   //   containing   an   error  
  }  
  在每一次i等于54时都将显示错误信息;
3、System.out.println可能会被缓冲,而System.err.println不会;
4、System.err和System.out就是错误输出和标准输出。如果你用LOG4J记录日志的话,且设定错误等级的话,System.err的输出是将记录到日志中;
5、输出设备是一样的,所以你看到的是一样的System.setErr()和System.setOut()是重定向两个流的方法。以下为Sun JDK1.5中文文档中的 可能有点泛泛了System.err“标准”错误输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标。按照惯例,此输出流用于显示错误消息,或者显示那些即使用户输出流(变量out的值)已经重定向到通常不被连续监视的某一文件或其他目标,也应该立刻引起用户注意的其他信息。System.out “标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标。
6、System.err.println()是要缓冲的,所以优先级会高点,而System.out.println()是不需要缓冲的,所以优先级会低点.
 */
	//消费者消费产品
	public synchronized void get() {
		while(!isProduce){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}	
		System.out.println("\t消费者吃了一个"+color+"的"+name);
		isProduce=false;
		notify();
	}
}

package com.cxl.thread.produce;
/**
 * 生产者线程
 * @author Administrator
 *
 */
public class Producer implements Runnable{

	private Storage storage;
	
	public Producer() {
		super();
	}

	public Producer(Storage storage) {
		super();
		this.storage = storage;
	}

	@Override
	public void run() {
		int num=0;
		while(true){
			if(num%2==0){
				//生产馒头
				storage.put("馒头","白色");
			}else{
				//生产玉米饼
				storage.put("玉米饼", "黄色");
			}
			num++;
		}		
	}	
}

package com.cxl.thread.produce;
/**
 * 消费者
 * @author Administrator
 *
 */
public class Consumer implements Runnable{

	private Storage storage;
	
	public Consumer(Storage storage) {
		super();
		this.storage = storage;
	}

	@Override
	public void run() {
		while(true){
			storage.get();
		}	
	}
}

package com.cxl.thread.produce;
/**
 * 测试
 * @author Administrator
 *
 */
public class test {

	public static void main(String[] args) {
		Storage storage=new Storage();
		Thread th1=new Thread(new Producer(storage));
		Thread th2=new Thread(new Consumer(storage));
		th1.start();
		th2.start();
	}
}


转载于:https://my.oschina.net/u/2339999/blog/412221

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值