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 create a java thread

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

Share

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

This article introduces the knowledge of "how to create a java thread". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What are the disadvantages of manually creating threads?

Uncontrolled risk

Frequent creation costs a lot

Uncontrolled risk

I believe you can also say one or two about this shortcoming.

System resources are limited, everyone can manually create threads for different businesses, and the creation criteria are different (for example, threads do not have a name). When the system is running, all threads are frantically grabbing resources, disorganized and undisciplined, and the chaotic scene can be imagined (when problems arise, they cannot be easily discovered and solved).

If there is a magical little partner who creates a thread for each request, when a large number of requests come, it is like a regular Trojan program, which runs out of memory mercilessly (you are ruthless, you are ruthless, you are unreasonable)

In addition, too many threads naturally cause the overhead of context switching.

Generally speaking, the risk of being uncontrolled is high.

Frequent creation costs a lot

Interview question: what is the problem with creating threads manually frequently?

A: it costs a lot.

This seems to be a correct answer that can be answered without thinking. Then I'm going to keep asking.

Interviewer: what does it cost to create a thread? How is it different from creating a normal Java object?

A:. Yes. Ah!

According to the usual understanding, creating a thread with new Thread () is no different from new Object (). Everything in Java connects objects, because the ancestor of Thread was also Object.

If you really understand it this way, it means that you don't have a good understanding of the thread life cycle. Please take a look back at the previous Java thread life cycle. This understanding is very simple.

In this article we make it clear that new Thread () does not create new threads at the operating system level, which is specific to the programming language. The real conversion to the operating system level to create a thread, but also to call the operating system kernel API, and then the operating system to allocate a series of resources to the thread

Without much nonsense, let's make a comparison between the two:

New Object () process

Object obj = new Object ()

When I need someone, I will give myself a new (I don't know if you are the same as me). You should be familiar with this process:

Allocate a piece of memory M

Initialize the object on memory M

Assign the address of memory M to the reference variable obj

It's that simple.

The process of creating a thread

As mentioned above, creating a thread also calls the operating system kernel API. To better understand the cost of creating and starting a thread, we need to look at what JVM does behind our backs:

It allocates memory for a thread stack that holds a stack frame for each thread method call

Each stack frame consists of an array of local variables, return values, Operand stack, and constant pool.

Some jvm that support native methods also assign a native stack

Each thread gets a program counter that tells it what instructions the current processor is executing.

The system creates a native thread corresponding to the Java thread

Add thread-related descriptors to the JVM internal data structure

Thread shared heap and method area

This description is a bit abstract, using data to show how much space it takes to create a thread (that is, to do nothing). The answer is about 1m.

Java-XX:+UnlockDiagnosticVMOptions-XX:NativeMemoryTracking=summary-XX:+PrintNMTStatistics-version

The picture above shows my test results with Java8. 19 threads are reserved and submitted by 19000+KB. On average, each thread needs about 1m in size. (the result of Java11 is completely different. Let's test this by ourselves.)

I'm sure you've learned by now that with stringent performance requirements, the cost of frequently manually creating / destroying threads is huge, and the solution is naturally the thread pool you know.

What is a thread pool?

Your common database connection pooling, instance pooling, and XX pooling, OO pooling, and various pooling are all pooling ideas, in short, the idea of managing resources together in order to maximize benefits and minimize risks.

Java also provides its own thread pool model-ThreadPoolExecutor. To apply the above pooling imagination, Java thread pool is to maximize the performance improvement brought about by high concurrency, minimize the risk of manually creating threads, and unify the management of multiple threads.

In order to understand this management idea, we only need to focus on the ThreadPoolExecutor constructor at present.

Public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory RejectedExecutionHandler handler) {if (corePoolSize < 0 | | maximumPoolSize

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