示例1:
package com.xx.job.day02;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
/**
* @ClassName ThreadPoolTest
* @Description
* @Author xuxiang
* @Date 11:46 2023/3/28
**/
@Slf4j(topic = "c.ThreadPoolTest")
public class ThreadPoolTest01 {
private static BlockingQueue blockingQueue1 = new ArrayBlockingQueue(100); // 有界
private static BlockingQueue blockingQueue2 = new SynchronousQueue(); // 为0
private static BlockingQueue blockingQueue3 = new LinkedBlockingDeque(100);// 默认无界,支持有界
private static BlockingQueue blockingQueue4 = new LinkedTransferQueue(); //无界
private static BlockingQueue blockingQueue5 = new LinkedBlockingQueue(100); // 默认无界,支持有界
private static BlockingQueue blockingQueue6 = new PriorityBlockingQueue(); // 无界队列
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
3000, TimeUnit.MICROSECONDS, blockingQueue1, new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws InterruptedException {
ConcurrentLinkedQueue<Integer> list = new ConcurrentLinkedQueue<>();
final CountDownLatch countDownLatch = new CountDownLatch(1000);
for (int i = 0; i < 1000; i++) {
log.debug("第{}个任务", i);
try {
FutureTask test = test(i, list,countDownLatch);
threadPoolExecutor.submit(test);
} catch (RejectedExecutionException e) {
e.printStackTrace();;
log.error("拒绝执行{}", i);
Thread.sleep(1000);
log.debug("继续执行{}", i);
FutureTask test = test(i,list,countDownLatch);
threadPoolExecutor.submit(test);
}
}
countDownLatch.await();
// threadPoolExecutor.shutdown();
log.info("list size:{},list:{}", list.size(), JSONUtil.toJsonStr(list));
}
public static FutureTask test(int index, ConcurrentLinkedQueue<Integer> list,CountDownLatch countDownLatch){
FutureTask futureTask = new FutureTask(() -> {
try {
Thread.sleep(100);
list.add(index);
countDownLatch.countDown();
} catch (InterruptedException e) {
log.error("InterruptedException", e);
}
log.debug("test run {}", index);
return null;
});
log.debug("队列剩余空间:{}", blockingQueue6.remainingCapacity());
return futureTask;
}
}
示例2:
package com.xx.job.day02;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
/**
* @ClassName ThreadPoolTest
* @Description
* @Author xuxiang
* @Date 11:46 2023/3/28
**/
@Slf4j(topic = "c.ThreadPoolTest")
public class ThreadPoolTest {
private static BlockingQueue blockingQueue1 = new ArrayBlockingQueue(100); // 有界
private static BlockingQueue blockingQueue2 = new SynchronousQueue(); // 为0
private static BlockingQueue blockingQueue3 = new LinkedBlockingDeque(100);// 默认无界,支持有界
private static BlockingQueue blockingQueue4 = new LinkedTransferQueue(); //无界
private static BlockingQueue blockingQueue5 = new LinkedBlockingQueue(100); // 默认无界,支持有界
private static BlockingQueue blockingQueue6 = new PriorityBlockingQueue(); // 无界队列
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
3000, TimeUnit.MICROSECONDS, blockingQueue1, new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(1000);
ConcurrentLinkedQueue<Integer> list = new ConcurrentLinkedQueue<>();
for (int i = 0; i < 1000; i++) {
log.debug("第{}个任务", i);
try {
test(i,list,countDownLatch);
} catch (RejectedExecutionException e) {
e.printStackTrace();;
log.error("拒绝执行{}", i);
Thread.sleep(1000);
log.debug("继续执行{}", i);
test(i,list,countDownLatch);
}
}
countDownLatch.await();
// threadPoolExecutor.shutdown();
log.info("list size:{},list:{}", list.size(), JSONUtil.toJsonStr(list));
}
public static void test(int index, ConcurrentLinkedQueue<Integer> list,CountDownLatch countDownLatch){
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
list.add(index);
countDownLatch.countDown();
} catch (InterruptedException e) {
log.error("InterruptedException", e);
}
log.debug("test run {}", index);
}
});
log.debug("队列剩余空间:{}", blockingQueue6.remainingCapacity());
}
}