【黑马程序员】Java进阶教程ELK高级搜索_ElasticStack技术栈 – 带源码课件

### 线程池高级配置与优化

Executor框架提供强大的线程管理能力。ThreadPoolExecutor核心参数包括corePoolSize、maximumPoolSize、keepAliveTime、workQueue和handler。合理配置这些参数对系统性能至关重要。

java 复制代码
java
下载
复制
运行
// 自定义线程池配置
public class ThreadPoolConfig {
    public static ThreadPoolExecutor createCustomPool() {
        int corePoolSize = Runtime.getRuntime().availableProcessors();
        int maxPoolSize = corePoolSize * 2;
        long keepAliveTime = 60L;
        
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(1000);
        ThreadFactory threadFactory = new CustomThreadFactory();
        RejectedExecutionHandler handler = new CustomRejectionHandler();
        
        return new ThreadPoolExecutor(
            corePoolSize, maxPoolSize, keepAliveTime,
            TimeUnit.SECONDS, workQueue, threadFactory, handler
        );
    }
}

// 自定义线程工厂
class CustomThreadFactory implements ThreadFactory {
    private final AtomicInteger count = new AtomicInteger(1);
    
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName("custom-pool-" + count.getAndIncrement());
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.setDaemon(false);
        return thread;
    }
}

// 自定义拒绝策略
class CustomRejectionHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        System.err.println("任务被拒绝: " + r.toString());
        // 可在此处添加日志记录或报警逻辑
    }
}

并发工具类实战应用

CountDownLatch用于等待多个线程完成,CyclicBarrier让线程互相等待,Semaphore控制并发访问数量,Exchanger实现线程间数据交换。

java 复制代码
java
下载
复制
运行
// 并发工具综合应用
public class ConcurrentToolsDemo {
    private final ExecutorService executor = Executors.newFixedThreadPool(4);
    private final CountDownLatch latch = new CountDownLatch(3);
    private final CyclicBarrier barrier = new CyclicBarrier(3);
    private final Semaphore semaphore = new Semaphore(2);
    
    public void executeTasks() {
        for (int i = 0; i < 3; i++) {
            executor.submit(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + " 获得许可");
                    
                    barrier.await(); // 等待所有线程就绪
                    // 执行具体任务
                    Thread.sleep(1000);
                    
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            });
        }
        
        try {
            latch.await(); // 等待所有任务完成
            System.out.println("所有任务完成");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
相关推荐
但要及时清醒6 分钟前
ArrayList和LinkedList
java·开发语言
一叶飘零_sweeeet15 分钟前
从测试小白到高手:JUnit 5 核心注解 @BeforeEach 与 @AfterEach 的实战指南
java·junit
摇滚侠31 分钟前
Spring Boot3零基础教程,Reactive-Stream 四大核心组件,笔记106
java·spring boot·笔记
Z3r4y32 分钟前
【代码审计】RuoYi-3.0 三处安全问题分析
java·web安全·代码审计·ruoyi-3.0
与遨游于天地1 小时前
Spring解决循环依赖实际就是用了个递归
java·后端·spring
陈果然DeepVersion1 小时前
Java大厂面试真题:Spring Boot+微服务+AI智能客服三轮技术拷问实录(六)
java·spring boot·redis·微服务·面试题·rag·ai智能客服
BeingACoder1 小时前
【SAA】SpringAI Alibaba学习笔记(一):SSE与WS的区别以及如何注入多个AI模型
java·笔记·学习·saa·springai
DolphinScheduler社区1 小时前
真实迁移案例:从 Azkaban 到 DolphinScheduler 的选型与实践
java·大数据·开源·任务调度·azkaban·海豚调度·迁移案例
zhangkaixuan4562 小时前
Apache Paimon 写入流程
java·大数据·apache·paimon