Power Node Java training tells you the versatile use of Java threads

Source: Internet
Author: User

Java development now is eight Immortals crossing recount Ah! Thinking back the current programming language is flagging, and my Java development, with the wind swept the leaves of the state, as the Java training industry, Huangpu Military Academy, in order to protect the future of Java, deliberately summed up some of the methods not known to people, Let your Java Development digest, baby go! Go! Go!

Let's go back to the thread class. Each thread in the program will have a state, either a user state or a daemon state. In other words, either the foreground thread or the background thread. The main thread is the user thread by default, and each new thread inherits the thread state from the thread that created it. So if you set a thread to be a daemon thread, all the threads it creates are flagged as daemons. If all the threads in the program are daemons, then the process terminates. We can view and set the thread state through the Boolean Setdaemon (True) and. Isdaemon methods.

When will the daemon thread be used?

If a process does not have to wait until a thread ends to terminate, the thread can be set as a daemon thread. This eliminates the hassle of gracefully shutting down threads and immediately ends the thread. In other words, if a thread that is performing an operation has to be shut down correctly or there will be bad consequences, then the thread should be the user thread. These are often critical transactions, such as database entries or updates, that are not interrupted.

Expert level

5. Processor Affinity (Processor Affinity)

This will be closer to the hardware, that is, when the software meets the hardware. Processor affinity allows you to bind a thread or process to a specific CPU core. This means that as long as it is a particular thread, it must be executed only on a specific CPU core. In general, how bindings are determined by the operating system's thread scheduler is based on its own logic, and it is likely to take into account the thread priorities we mentioned earlier.

The benefit of doing this is CPU caching. If a thread only runs on a core, the probability of its data being in the cache is greatly increased. If the data is exactly in the CPU cache, then there is no need to re-download it from memory. The milliseconds you save can be used on the edge of the blade, where the code can start executing immediately and make better use of the CPU time allocated to it. Of course, there may be some optimizations at the operating system level, and the hardware architecture is certainly an important factor, but leveraging the affinity of the processor can at least reduce the chance of threads switching CPUs.

Because of the many factors that are mixed here, how much processor affinity affects throughput, it is best to prove it in a test way. Perhaps this method does not always significantly improve performance, but at least one benefit is that throughput is relatively stable. Affinity strategies can be refined to very granular granularity, depending on what you specifically want. The high-frequency trading industry is one of the most capable scenarios of this strategy.

Processor Affinity Testing

Java has no native support for processor affinity, and of course, the story hasn't ended yet. On Linux, we can use the Taskset command to set the affinity of the process. Suppose we now have a Java process running, and we want to bind it to a specific CPU:

Taskset-c1 "Java abouttobepinned"

If it is a process that is already running:

Taskset-c 1 <pid

You'll need to add some code to get to the thread level. Fortunately, there is an open source library that can do this: java-thread-affinity. The library was developed by Peter Lawrey of OPENHFT, and the simplest and most straightforward way to do this is to use the library. Let's take a quick look at how to bind a thread with an example, and refer to its documentation on GitHub for more details about the library:

Affinitylock al = Affinitylock.acquirelock;

That's all you can do. Some of the more advanced options for acquiring locks--such as choosing cpu--based on different strategies--are described in detail on GitHub.

Conclusion

In this article we introduce 5 points of knowledge about threads: Thread name, thread local storage, priority, daemon thread, and processor affinity. Hopefully this will open up a new window for the content you use in your daily work.

Power Node Java training tells you the versatile use of Java threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.