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 determine the number of concurrent threads in java

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

Share

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

This article mainly explains "how to determine the number of concurrent threads in java". 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 how to determine the number of concurrent threads in java.

Determination of the number of concurrent threads in java

This paper discusses the basis for determining the number of threads from the point of view of control variables. The model is very simple, and in the actual production environment, the situation must be much more complicated than the following. Should be fully tested in order to optimize the number of threads.

There are roughly two types of java applications: cpu-intensive and io-intensive.

Cpu intensive

It means that threads spend most of their time using cpu. Generally speaking, ordinary operations need to use cpu, such as calculation, reading, looping, assignment, query, sorting, and so on. In the best case, Daniel recommends setting the number of threads to count (cpu) +-1.

Io intensive

Generally speaking, io operation does not require the participation of cpu. When a thread is in io, the thread will be blocked (one of the six states of the thread is Blocked). If a thread needs 100ms to complete a certain task, io needs 80ms CPU and 20ms (ignore other times). Then the number of threads should be set to 5.

A situation in which there is a lock

Multithreading is often locked for the sake of safety. For critical code (code that is called frequently), it can often be one of the bases for the number of threads.

For global locks (such as static locks), the code executes serially no matter how many threads there are. In this way, the more threads, the worse. The smaller the lock granularity, the more beneficial it is for thread concurrency. For example, for ConcurrentHashMap, there are 16 segment, that is, 16 locks.

In a rational case, the lock granularity can be reduced by 16 times, then 16 concurrency can be allowed naturally. In the worst-case scenario, 16 threads compete for an segment. The number of threads needs to be tuned according to the actual situation.

The principle of how to determine the number of threads in java thread pool that multi-threads can execute tasks quickly

Because the server has multiple processor cores. When running a process, if there is only one thread, only one processor core can be mobilized, and the other processor cores may be idle. If it is multithreaded, you can call multiple processor cores to handle tasks with maximum efficiency.

Parameters required to create a thread pool

The parameters needed to create a thread pool are: number of core threads, maximum number of threads, thread destruction time, task queue, rejection policy, and so on.

There are two types of threads in the thread pool, which are core threads and non-core threads. When a task is received by the thread pool, the number of core threads is first created to process the task until the number of tasks to be processed exceeds the sum of the task queue length and the number of core threads, and non-core threads continue to be created until the maximum number of threads.

When the number of tasks received by the thread pool is about to exceed the sum of the task queue length and the maximum number of threads, a deny policy is triggered to process the task.

The non-core thread is destroyed immediately after execution is completed, while the core thread waits for the set destroy time before it is destroyed.

When the task queue length is large enough, the number of core threads is equal to the maximum number of threads, otherwise it cannot be triggered to create a non-core thread

Determine the number of threads

The formula for calculating the number of threads is:

Nthreads=NcpuUcpu (1+w/c) = Ncpu* (1+w/c)

Where Nthreads: number of threads; Ncpu: number of processor cores; Ucpu: percentage of processor usage; Whip C: ratio of wait time to computing time

The Ncpu can be obtained from the following code

Runtime.getRuntime () .availableProcessors ()

The ratio of waiting time to computing time

For IO-intensive, blocking time w is generally several times longer than computing time c. Assuming blocking time = computing time, Nthreads=Ncpu* (1) = 2Ncpu. So in this case, consider twice the number of CPU cores as the number of threads

For the computationally intensive, the blocking time tends to be zero, that is, the wmax c tends to zero, and the formula Nthreads = Ncpu.

The number of threads is generally an integral multiple of the number of processor cores. If the number of threads is set too much, in the case of multi-task concurrency, it will affect the overall running speed of the server; if the setting is too few, it will not maximize the performance of the application server. Therefore, it needs to be adjusted according to the specific business.

At this point, I believe you have a deeper understanding of "how to determine the number of concurrent threads in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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