当前位置 : 主页 > 编程语言 > c++ >

JAVA多线程基础

来源:互联网 收集:自由互联 发布时间:2021-06-30
阻塞队列 package thread.blockqueue;import java.util.LinkedList;import java.util.List;/** * 阻塞队列 * * @author Administrator * */public class BlockingQueue {// 构建队列private List queue = new LinkedList(); // 队列容量 priv
阻塞队列
package thread.blockqueue;

import java.util.LinkedList;

import java.util.List;

/**
 * 阻塞队列
 * 
 * @author Administrator
 * 
 */
public class BlockingQueue
 
   {
	// 构建队列
	private List
  
    queue = new LinkedList<>(); // 队列容量 private int limit = 10; public BlockingQueue(int limit) { this.limit = limit; } /** * 进队列 * * @param item * @throws InterruptedException */ public synchronized void enqueue(E item) throws InterruptedException { while (this.queue.size() == this.limit) wait(); if (this.queue.size() == 0) notifyAll(); this.queue.add(item); } /** * 出队列 * * @return * @throws InterruptedException */ public synchronized E dequeue() throws InterruptedException { while (this.queue.size() == 0) wait(); if (this.queue.size() == limit) { notifyAll(); } return this.queue.remove(0); } }
  
 
网友评论