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

How to use ThreadPoolExecutor thread pool correctly

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to correctly use ThreadPoolExecutor thread pool, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Import java.util.Random

Import java.util.concurrent.*

Public class PoolTest {

Private BlockingQueue workingQueue = new ArrayBlockingQueue (10)

Private RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy ()

Private ExecutorService threadPool = new ThreadPoolExecutor (10,10,0L, TimeUnit.MILLISECONDS, workingQueue, rejectedExecutionHandler)

Private Random random = new Random ()

Public void test () {

Int size = 10

Final CountDownLatch latch = new CountDownLatch (size)

For (int I = 0; I < size; I + +) {

ThreadPool.execute (new Runnable () {

Int x = random.nextInt (10)

@ Override

Public void run () {

Try {

Plus (xmeme x)

} finally {

Latch.countDown ()

}

}

});

}

Try {

Latch.await ()

} catch (InterruptedException e) {

E.printStackTrace ()

} finally {

System.out.println ("Thread execution completed")

}

ThreadPool.shutdown ()

}

Public void plus (int x, int y) {

System.out.println ("sum =" + (x + y))

}

Public static void main (String [] args) {

PoolTest poolTest = new PoolTest ()

PoolTest.test ()

}

}

After reading the above, have you mastered how to use the ThreadPoolExecutor thread pool correctly? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report