In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "multithreading practice analysis in java project". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the "multithreading practice Analysis in java Project".
In project development, multithreading is needed for some data processing, such as batch upload of files, batch writing of database, segmented download of large files, and so on. Spring's own thread pool processing is usually used to achieve customized processing and better control of threads. It is recommended to use a custom thread pool. The main points involved are:
1. Custom thread factory (ThreadFactoryBuilder), mainly used for thread naming, easy to trace
two。 Custom thread pool (ThreadPoolExecutorUtils), which can optimize configuration parameters according to function
3. An abstract multithreaded task processing interface (OperationThreadService) and general implementation (OperationThread)
4. Unified scheduling implementation (MultiThreadOperationUtils)
Core idea: divide and conquer and merge, each thread calculates its own results, and finally unifies the summary.
Import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.concurrent.ThreadFactory;import java.util.concurrent.atomic.AtomicLong;/** * description: a custom thread pool that follows the alibaba programming specification. Use ThreadPoolExecutor to create a thread pool using * to set a more descriptive thread name. The default ThreadFactory, which gives a thread a name of pool-m-thread-n, such as pool-1-thread-1. * when analyzing a thread dump, it is difficult to know the purpose of the thread. A descriptive thread name is needed to analyze the tracking problem. * set whether the thread is a daemon thread, the default ThreadFactory always submits the non-daemon thread * set the thread priority, and the default ThreadFactory is always the general priority thread submitted *
* the CustomThreadFactoryBuilder class implements an elegant Builder Mechanism way to get a custom ThreadFactory instance. * there is a method newThread (Runnable r) in the ThreadFactory interface that accepts Runnable type parameters. * the factory logic of the business should be written in this method to configure attributes such as thread name, priority, daemon thread status, and so on. * original link: https://blog.csdn.net/zombres/article/details/80497515 * * @ author Hlingoes * @ date 0:45 on 2019-12-22 * / public class ThreadFactoryBuilder {private static Logger logger = LoggerFactory.getLogger (ThreadFactoryBuilder.class); private String nameFormat = null; private boolean daemon = false; private int priority = Thread.NORM_PRIORITY; public ThreadFactoryBuilder setNameFormat (String nameFormat) {if (nameFormat = = null) {throw new NullPointerException () } this.nameFormat = nameFormat; return this;} public ThreadFactoryBuilder setDaemon (boolean daemon) {this.daemon = daemon; return this;} public ThreadFactoryBuilder setPriority (int priority) {if (priority)
< Thread.MIN_PRIORITY) { throw new IllegalArgumentException(String.format( "Thread priority (%s) must be >=% s ", priority, Thread.MIN_PRIORITY);} if (priority > Thread.MAX_PRIORITY) {throw new IllegalArgumentException (" Thread priority (% s) must be)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.