有限BlockingQueue资源在多线程池ThreadPoolExector中的控制和处理

有时候我们需要处理类似这样的问题

1. 队列资源是有限的,一条队列里只能有10个位置,当满了后,后面来的需要等待前面的处理完有空位后再被处理。

2. 处理是多线程的,都在读这个队列


在不考滤第三方队列,单纯用JDK支持的BlockingQueue和ThreadPoolExecutor实现。可以是:

1.限制BQ的大小,每个新的item通过put放法放入BQ中,这个方法的意思是如果满了就会等空位

2.把BQ放到ThreadPoolExecutor中, 然后启动 threadPoolExecutor中的work去执行这些Runnable 

3.  需要调 用 prestartAllCoreThreads 来使线程work动作起来处理队列里的请求

下面是一个例子:

public class BlockingQueueTest {

	public static void main(String[] args) throws InterruptedException {
		BlockingQueue sharedQueue = new LinkedBlockingQueue<>(10); //capacity
		ThreadPoolExecutor eService =  new ThreadPoolExecutor(3, 3,
                0L, TimeUnit.MILLISECONDS,
                sharedQueue);
		eService.prestartAllCoreThreads();
		for(int i=0;i<100;i++){
			System.out.println(" before current:"+i+",queue size:"+sharedQueue.size());
			Runnable task=()->{
				try {
					Thread.sleep(10);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println(" queue size:"+sharedQueue.size());
			};
			sharedQueue.put(task);
			System.out.println(" after current:"+i+",queue size:"+sharedQueue.size());
		}
		eService.shutdown();
	}

}

输出结果如果,可以看得出queue size最大就是10了,因为是用了put :

 before current:0,queue size:0
 after current:0,queue size:1
 before current:1,queue size:0
 after current:1,queue size:1
 before current:2,queue size:0
 after current:2,queue size:0
 before current:3,queue size:0
 after current:3,queue size:1
 before current:4,queue size:1
 after current:4,queue size:2
 before current:5,queue size:2
 after current:5,queue size:3
 before current:6,queue size:3
 after current:6,queue size:4
 before current:7,queue size:4
 after current:7,queue size:5
 before current:8,queue size:5
 after current:8,queue size:6
 before current:9,queue size:6
 after current:9,queue size:7
 before current:10,queue size:7
 after current:10,queue size:8
 before current:11,queue size:8
 after current:11,queue size:9
 before current:12,queue size:9
 after current:12,queue size:10
 before current:13,queue size:10
 queue size:10
 queue size:10
 after current:13,queue size:10
 before current:14,queue size:9
 after current:14,queue size:10
 before current:15,queue size:10
 queue size:10
 after current:15,queue size:10
 before current:16,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:16,queue size:10
 before current:17,queue size:8
 after current:17,queue size:9
 before current:18,queue size:9
 after current:18,queue size:10
 before current:19,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:19,queue size:10
 before current:20,queue size:8
 after current:20,queue size:9
 before current:21,queue size:9
 after current:21,queue size:10
 before current:22,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:22,queue size:9
 before current:23,queue size:8
 after current:23,queue size:9
 before current:24,queue size:9
 after current:24,queue size:10
 before current:25,queue size:10
 queue size:10
 queue size:10
 after current:25,queue size:9
 queue size:10
 before current:26,queue size:9
 after current:26,queue size:9
 before current:27,queue size:9
 after current:27,queue size:10
 before current:28,queue size:10
 queue size:10
 queue size:10
 after current:28,queue size:10
 before current:29,queue size:9
 after current:29,queue size:10
 before current:30,queue size:10
 queue size:10
 after current:30,queue size:10
 before current:31,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:31,queue size:8
 before current:32,queue size:8
 after current:32,queue size:9
 before current:33,queue size:9
 after current:33,queue size:10
 before current:34,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:34,queue size:10
 before current:35,queue size:8
 after current:35,queue size:9
 before current:36,queue size:9
 after current:36,queue size:10
 before current:37,queue size:10
 queue size:10
 queue size:10
 after current:37,queue size:9
 before current:38,queue size:9
 queue size:10
 after current:38,queue size:10
 before current:39,queue size:9
 after current:39,queue size:10
 before current:40,queue size:10
 queue size:10
 queue size:10
 after current:40,queue size:9
 before current:41,queue size:9
 after current:41,queue size:10
 before current:42,queue size:10
 queue size:10
 after current:42,queue size:10
 before current:43,queue size:10
 queue size:10
 queue size:10
 after current:43,queue size:10
 before current:44,queue size:9
 after current:44,queue size:10
 before current:45,queue size:10
 queue size:10
 after current:45,queue size:10
 before current:46,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:46,queue size:10
 before current:47,queue size:8
 after current:47,queue size:9
 before current:48,queue size:9
 after current:48,queue size:10
 before current:49,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:49,queue size:10
 before current:50,queue size:8
 after current:50,queue size:9
 before current:51,queue size:9
 after current:51,queue size:10
 before current:52,queue size:10
 queue size:10
 queue size:10
 after current:52,queue size:10
 before current:53,queue size:9
 queue size:10
 after current:53,queue size:10
 before current:54,queue size:9
 after current:54,queue size:10
 before current:55,queue size:10
 queue size:10
 queue size:9
 queue size:9
 after current:55,queue size:8
 before current:56,queue size:8
 after current:56,queue size:9
 before current:57,queue size:9
 after current:57,queue size:10
 before current:58,queue size:10
 queue size:10
 queue size:10
 after current:58,queue size:9
 before current:59,queue size:9
 after current:59,queue size:10
 before current:60,queue size:10
 queue size:10
 after current:60,queue size:10
 before current:61,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:61,queue size:10
 before current:62,queue size:8
 after current:62,queue size:9
 before current:63,queue size:9
 after current:63,queue size:10
 before current:64,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:64,queue size:10
 before current:65,queue size:8
 after current:65,queue size:9
 before current:66,queue size:9
 after current:66,queue size:10
 before current:67,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:67,queue size:9
 before current:68,queue size:8
 after current:68,queue size:9
 before current:69,queue size:9
 after current:69,queue size:10
 before current:70,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:70,queue size:9
 before current:71,queue size:8
 after current:71,queue size:9
 before current:72,queue size:9
 after current:72,queue size:10
 before current:73,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:73,queue size:10
 before current:74,queue size:8
 after current:74,queue size:9
 before current:75,queue size:9
 after current:75,queue size:10
 before current:76,queue size:10
 queue size:10
 queue size:10
 after current:76,queue size:10
 before current:77,queue size:9
 queue size:10
 after current:77,queue size:10
 before current:78,queue size:9
 after current:78,queue size:10
 before current:79,queue size:10
 queue size:10
 queue size:10
 after current:79,queue size:10
 before current:80,queue size:9
 queue size:10
 after current:80,queue size:10
 before current:81,queue size:9
 after current:81,queue size:10
 before current:82,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:82,queue size:10
 before current:83,queue size:8
 after current:83,queue size:9
 before current:84,queue size:9
 after current:84,queue size:10
 before current:85,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:85,queue size:9
 before current:86,queue size:8
 after current:86,queue size:9
 before current:87,queue size:9
 after current:87,queue size:10
 before current:88,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:88,queue size:10
 before current:89,queue size:8
 after current:89,queue size:9
 before current:90,queue size:9
 after current:90,queue size:10
 before current:91,queue size:10
 queue size:10
 queue size:10
 after current:91,queue size:9
 before current:92,queue size:9
 after current:92,queue size:10
 before current:93,queue size:10
 queue size:10
 after current:93,queue size:10
 before current:94,queue size:10
 queue size:10
 queue size:10
 queue size:10
 after current:94,queue size:10
 before current:95,queue size:8
 after current:95,queue size:9
 before current:96,queue size:9
 after current:96,queue size:10
 before current:97,queue size:10
 queue size:10
 queue size:9
 queue size:8
 after current:97,queue size:8
 before current:98,queue size:8
 after current:98,queue size:9
 before current:99,queue size:9
 after current:99,queue size:10
 queue size:10
 queue size:10
 queue size:8
 queue size:7
 queue size:7
 queue size:5
 queue size:4
 queue size:4
 queue size:2
 queue size:1
 queue size:1
 queue size:0
 queue size:0

你可能感兴趣的:(Java,Common)