incubator-streampark icon indicating copy to clipboard operation
incubator-streampark copied to clipboard

[improve] Thread pool usage improvement

Open magestacks opened this issue 3 years ago • 1 comments

Search before asking

  • [X] I had searched in the feature and found no similar feature requirement.

Description

the current way to create a thread pool

private final ExecutorService executorService = new ThreadPoolExecutor(
    Runtime.getRuntime().availableProcessors() * 5,
    Runtime.getRuntime().availableProcessors() * 10,
    60L,
    TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(1024),
    ThreadUtils.threadFactory("streampark-cluster-executor"),
    new ThreadPoolExecutor.AbortPolicy()
);

suggestions below

maxPoolSize:

corePoolSize + (corePoolSize >> 1)

LinkedBlockingQueue capacity: 4096

RejectedExecutionHandler:

public class RunsOldestTaskPolicy implements RejectedExecutionHandler {

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if (executor.isShutdown()) {
            return;
        }
        BlockingQueue<Runnable> workQueue = executor.getQueue();
        Runnable firstWork = workQueue.poll();
        boolean newTaskAdd = workQueue.offer(r);
        if (firstWork != null) {
            firstWork.run();
        }
        if (!newTaskAdd) {
            executor.execute(r);
        }
    }
}

Usage Scenario

No response

Related issues

No response

Are you willing to submit a PR?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

magestacks avatar Sep 11 '22 10:09 magestacks

thanks your feedback. Can you improve this issue?

wolfboys avatar Sep 11 '22 12:09 wolfboys