"The cost of Java multi-threading"

Source: Internet
Author: User
Tags switches

Original link: http://tutorials.jenkov.com/java-concurrency/costs.html

Jakob Jenkov translation: Ancient Sheng Chang proofreading: Ouzhengkong

Java's application from a single thread to a multithreaded application is not just a benefit, it also has some cost. Don't use multithreading just to use multithreading. It should be clear that multithreading is used when multiple threads can be used more often than the cost of paying. If you have questions in Java learning, you should try to measure the performance and responsiveness of your application, not just guesswork.

More complex design

While there are several multithreaded applications that are simpler than single-threaded applications, others are generally more complex. This part of the code requires special attention when accessing shared data in multi-threaded access. The interaction between threads is often very complex. Incorrect thread synchronization produces errors that are very difficult to discover and reproduce to fix.

The cost of context switching

When the CPU switches from executing a thread to executing another thread, it needs to store the local data of the current thread, the program pointer, and so on, then load the local data of the other thread, the program pointer, and so on, before it starts executing. This switchover is called a "context switch". The CPU executes one thread in one context and then switches to another in the context of executing another thread.

Context switching is not cheap. If it is not necessary, you should reduce the occurrence of context switches.

You can read more about context switching through Wikipedia:

Http://en.wikipedia.org/wiki/Context_switch

Increase resource consumption

Threads need to get some resources from the computer while they are running. In addition to the CPU, the thread also needs some memory to maintain its local stack. It also needs to occupy some resources in the operating system to manage threads. We can try to write a program that lets it create 100 threads, and these threads do nothing, just wait and see how much memory the program consumes when it runs.

"The cost of Java multi-threading"

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.