Java threads learning 12-- case - blocking queue BlockingQueue

       Java.util.concurrent package is a powerful package!  

        This time my job is to improve the company's scheduler, scheduler schedules thread pool tasks , production tasks producers, consumer spending task, then you need a task queue, the queue producers insert task, consumer were extracted from the task execution queue, the queue scheduler in by BlockingQueue achieve, then a small check, under the surface and see BlockingQueue principles and methods .
          BlockingQueue have one of four final status, an exception is thrown, it returns the special value, blocking, timeouts, the following table summarizes these methods:

  Throw an exception The special value Clog time out
insert add(e) offer(e) put(e) offer(e, time, unit)
Remove remove() poll() take() poll(time, unit)
an examination element() peek() unavailable unavailable

Is BlockingQueue interface implementation class are as follows:

       1. ArrayBlockQueue: backed by an array of a bounded blocking queue. This queue FIFO (first in first out) principle sort elements. Creates its object must clear size, like an array.

       2. LinkedBlockQueue: a blocking queue size may vary. This queue FIFO (first in first out) principle sort elements. If not explicitly create an object that size, the default is Integer.MAX_VALUE. Throughput link is usually higher than the queue based on queue array, but in most concurrent applications, it may be less predictable performance lower. 

       3. PriorityBlockingQueue: similar LinkedBlockingQueue, but not the contained object sorting FIFO, but according to the natural order of the sort order of the objects carried or constructor Comparator determined.

       4. SynchronousQueue: synchronous queue. No synchronous queue capacity, each insert must wait for the other thread is removed, and vice versa.

 

       Achieved through the use of the previously achieved following ArrayBlockQueue producer consumption / costs by mode, as follows:

/** 定义一个盘子类,可以放鸡蛋和取鸡蛋 */
public class BigPlate {
 
	/** 装鸡蛋的盘子,大小为5 */
	private BlockingQueue<Object> eggs = new ArrayBlockingQueue<Object>(5);
	
	/** 放鸡蛋 */
	public void putEgg(Object egg) {
		try {
			eggs.put(egg);// 向盘子末尾放一个鸡蛋,如果盘子满了,当前线程阻塞
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
 
		// 下面输出有时不准确,因为与put操作不是一个原子操作
		System.out.println("放入鸡蛋");
	}
	
	/** 取鸡蛋 */
	public Object getEgg() {
		Object egg = null;
		try {
			egg = eggs.take();// 从盘子开始取一个鸡蛋,如果盘子空了,当前线程阻塞
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
 
		// 下面输出有时不准确,因为与take操作不是一个原子操作
		System.out.println("拿到鸡蛋");
		return egg;
	}
	
	/** 放鸡蛋线程 */
	static class AddThread extends Thread {
		private BigPlate plate;
		private Object egg = new Object();
 
		public AddThread(BigPlate plate) {
			this.plate = plate;
		}
 
		public void run() {
			plate.putEgg(egg);
		}
	}
 
	/** 取鸡蛋线程 */
	static class GetThread extends Thread {
		private BigPlate plate;
 
		public GetThread(BigPlate plate) {
			this.plate = plate;
		}
 
		public void run() {
			plate.getEgg();
		}
	}
	
	public static void main(String[] args) {
		BigPlate plate = new BigPlate();
		// 先启动10个放鸡蛋线程
		for(int i = 0; i < 10; i++) {
			new Thread(new AddThread(plate)).start();
		}
		// 再启动10个取鸡蛋线程
		for(int i = 0; i < 10; i++) {
			new Thread(new GetThread(plate)).start();
		}
	}
}

 

执行结果:

放入鸡蛋
放入鸡蛋
放入鸡蛋
放入鸡蛋
放入鸡蛋
拿到鸡蛋
放入鸡蛋
拿到鸡蛋
拿到鸡蛋
拿到鸡蛋
放入鸡蛋
放入鸡蛋
放入鸡蛋
拿到鸡蛋
放入鸡蛋
拿到鸡蛋
拿到鸡蛋
拿到鸡蛋
拿到鸡蛋
拿到鸡蛋

           Looking at the results, put the eggs start 10 threads and 10 threads take the eggs, add eggs before 5 threads successful implementation, the sixth, was found full of dishes, blocking live, then switch to take the egg thread execution, success realized the producer / consumer model. java.util.concurrent package is a powerful package!

 

 

Guess you like

Origin blog.csdn.net/yuhaibao324/article/details/93150473