Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Java thread pool optimization?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the method of Java thread pool optimization". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what is the method of Java thread pool optimization" can help you solve the problem.

Optimization of upgraded Thread Pool

1: four new rejection policies have been added. They are: MyAbortPolicy, MyDiscardPolicy, MyDiscardOldestPolicy, MyCallerRunsPolicy

2: the construction method of thread pool MyThreadPoolExecutor is optimized, and the parameter check is added to prevent the mistransmission of parameters.

3: this is the most important optimization.

Removes thread warm-up from the thread pool. Because thread warm-up consumes a lot of memory, we always run when we don't use the thread pool.

In exchange, when we call the execute method to add tasks, we compare the current size of the workers thread collection with the value of corePoolSize, and then use new MyWorker () to create add threads to the thread pool. The advantage is that when we create a thread pool, if we don't use it, it will have no effect on the current memory. When we use it, we will create a thread and put it into the thread pool for reuse.

Thread pool constructor public MyThreadPoolExecutor () {this (5 new ArrayBlockingQueue (10), Executors.defaultThreadFactory (), defaultHandle);} public MyThreadPoolExecutor (int corePoolSize, BlockingQueue waitingQueue,ThreadFactory threadFactory) {this (corePoolSize,waitingQueue,threadFactory,defaultHandle);} public MyThreadPoolExecutor (int corePoolSize, BlockingQueue waitingQueue,ThreadFactory threadFactory,MyRejectedExecutionHandle handle) {this.workers=new HashSet (corePoolSize) If (corePoolSize > = 0 invalid thread pool rejection null) {this.corePoolSize=corePoolSize; this.waitingQueue=waitingQueue; this.threadFactory=threadFactory; this.handle=handle;} else {throw new NullPointerException ("invalid thread pool parameter");} thread pool rejection policy

Policy API: MyRejectedExecutionHandle

Package com.springframework.concurrent;/** * Custom rejection Policy * @ author Youzheng Jie * / public interface MyRejectedExecutionHandle {void rejectedExecution (Runnable runnable,MyThreadPoolExecutor threadPoolExecutor);}

Policy internal implementation class

/ * implement custom rejection policy * / / throw exception policy (default) public static class MyAbortPolicy implements MyRejectedExecutionHandle {public MyAbortPolicy () {} @ Override public void rejectedExecution (Runnable r, MyThreadPoolExecutor t) {throw new MyRejectedExecutionException ("task->" + r.toString () + "thread pool->" + t.toString () + "reject") Silent discarding policy public static class MyDiscardPolicy implements MyRejectedExecutionHandle {public MyDiscardPolicy () {} @ Override public void rejectedExecution (Runnable runnable, MyThreadPoolExecutor threadPoolExecutor) {}} / / discard the oldest task policy public static class MyDiscardOldestPolicy implements MyRejectedExecutionHandle {public MyDiscardOldestPolicy () {} @ Override public void rejectedExecution (Runnable runnable) MyThreadPoolExecutor threadPoolExecutor) {if (! threadPoolExecutor.isShutdown ()) {/ / if the thread pool is not closed threadPoolExecutor.getWaitingQueue () .poll () / / lose the oldest task, and there will be a place for the new task threadPoolExecutor.execute (runnable) / / add a new task to the queue} / / the caller invokes the policy public static class MyCallerRunsPolicy implements MyRejectedExecutionHandle {public MyCallerRunsPolicy () {} @ Override public void rejectedExecution (Runnable runnable, MyThreadPoolExecutor threadPoolExecutor) {if (! threadPoolExecutor.isShutdown ()) {/ / determine whether the thread pool is closed runnable.run () }}}

Encapsulation rejection method

Protected final void reject (Runnable runnable) {this.handle.rejectedExecution (runnable, this);} protected final void reject (Runnable runnable,MyThreadPoolExecutor threadPoolExecutor) {this.handle.rejectedExecution (runnable, threadPoolExecutor);} execute method @ Override public boolean execute (Runnable runnable) {if (! this.waitingQueue.offer (runnable)) {this.reject (runnable); return false } else {if (this.workers.size () = 0 invalid thread pool parameters, invalid thread pool parameters) {this.corePoolSize=corePoolSize; this.waitingQueue=waitingQueue; this.threadFactory=threadFactory; this.handle=handle;} else {workers.size ("invalid thread pool parameters") }} / * implement custom rejection policy * / / throw exception policy (default) public static class MyAbortPolicy implements MyRejectedExecutionHandle {public MyAbortPolicy () {} @ Override public void rejectedExecution (Runnable r, MyThreadPoolExecutor t) {throw new MyRejectedExecutionException ("task->" + r.toString () + "thread pool->" + t.toString () + "reject") Silent discarding policy public static class MyDiscardPolicy implements MyRejectedExecutionHandle {public MyDiscardPolicy () {} @ Override public void rejectedExecution (Runnable runnable, MyThreadPoolExecutor threadPoolExecutor) {}} / / discard the oldest task policy public static class MyDiscardOldestPolicy implements MyRejectedExecutionHandle {public MyDiscardOldestPolicy () {} @ Override public void rejectedExecution (Runnable runnable) MyThreadPoolExecutor threadPoolExecutor) {if (! threadPoolExecutor.isShutdown ()) {/ / if the thread pool is not closed threadPoolExecutor.getWaitingQueue () .poll () / / lose the oldest task, and there will be a place for the new task threadPoolExecutor.execute (runnable) / / add a new task to the queue} / / the caller invokes the policy public static class MyCallerRunsPolicy implements MyRejectedExecutionHandle {public MyCallerRunsPolicy () {} @ Override public void rejectedExecution (Runnable runnable, MyThreadPoolExecutor threadPoolExecutor) {if (! threadPoolExecutor.isShutdown ()) {/ / determine whether the thread pool is closed runnable.run () }} / / call reject method protected final void reject (Runnable runnable) {this.handle.rejectedExecution (runnable, this);} protected final void reject (Runnable runnable,MyThreadPoolExecutor threadPoolExecutor) {this.handle.rejectedExecution (runnable, threadPoolExecutor);} / * MyWorker is each of our thread objects * / private final class MyWorker implements Runnable {final Thread thread / / for each MyWorker MyWorker () {Thread td = threadFactory.newThread (this); td.setName (THREADPOOL_NAME+threadNumber.getAndIncrement ()); this.thread=td; this.thread.start (); workers.add (this) } / / execute task @ Override public void run () {/ / cycle receive task while (true) {/ / Loop exit condition: / / 1: when isRunning is false and the queue size of waitingQueue is 0 (that is, no task), it will exit gracefully. / / 2: when STOPNOW is true, the shutdownNow method is called to exit violently. If ((! isRunning&&waitingQueue.size () = = 0) | | STOPNOW) {break;} else {/ / keep fetching tasks as tasks! When = null, call the run method to handle the task Runnable runnable = waitingQueue.poll (); if (runnableinvalid null) {runnable.run (); System.out.println ("task== >" + taskcount.incrementAndGet ()) }} / / put the task @ Override public boolean execute (Runnable runnable) {if (! this.waitingQueue.offer (runnable)) {this.reject (runnable); return false in the thread pool } else {if (this.workers.size ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report