Java multi-thread Performance Analysis

Source: Internet
Author: User

today, I saw the previous post on je. For the algorithm for the sum of hundreds of millions of data, multithreading is fast or slow, I think it depends on the situation.
1. single-core, no I/O, network and other resource operations
result: multithreading is slower than single-thread theory
cause: the multi-thread startup thread consumes CPU resources, multithreading only splits the computing process. At the same time, only one thread occupies the CPU. Therefore, multithreading not only consumes resources at startup, but also consumes time during thread scheduling. Therefore, a single thread is faster than multithreading theory.
2. single-core, Io, network, and other resource operations
result: multithreading is faster than single-thread theory
cause: if the hundreds of millions of data needs to be obtained from files or other places on the Network (databases), Io and network operations are much slower than CPU operations, therefore, the CPU waits for Io operations. When multiple threads are used, other threads can perform computation when one thread Io reads some data. A single thread needs to wait until the file is read. Therefore, multithreading is faster in theory.
3.
result: uncertain
cause: In most operating systems, multiple Threads of a process can be allocated to multiple CPUs for parallel processing. Therefore, multithreading is faster than a single thread in theory; in some operating systems, multiple threads of a process are not supported to be allocated to multiple CPUs for parallel processing. According to the result of 1, multithreading is theoretically slower than that of a single thread.
4. multi-core, Io, network, and other resource operations
result: multithreading is faster than single-thread theory
cause: if the operating system supports parallel processing of multiple threads of a process allocated to multiple CPUs, multithreading is faster than single thread theory.
If the operating system does not support parallel processing of multiple threads in a single process allocated to multiple CPUs, multithreading is faster than the single thread theory. It is obtained based on the result of 2.

An example of the masters in naje: Dig a cave and I will expand it.
Single-core, single-thread, IO: It is equivalent to digging from one side of the mountain. The worker needs to dig for 5 minutes, and then spend 5 minutes to transport the Earth out with a car.
Single-core, multi-thread, IO: It is equivalent to digging from one side of the mountain. It takes 5 minutes for the worker to dig and then 5 minutes to use a car to transport the earth, another worker began mining;

Dual-core, single-thread, IO: it is equivalent to a worker on both sides of the mountain to perform mining. The worker needs to dig for 5 minutes, and then spend 5 minutes to transport the Earth with a car;
Dual-core, multi-thread, IO: It is equivalent to digging from two workers on both sides of the mountain. It takes 5 minutes for the worker to dig and then 5 minutes to use a car to transport the earth;

Single-core, single-threaded, and Io-free: it is equivalent to a worker mining from the mountain side.
Single-core, multi-thread, and no I/O: It is equivalent to two workers digging from one side of the mountain, one digging, the other rest, and taking turns.

Dual-core, single-threaded, and Io-free: it is equivalent to a worker working on both sides of the mountain for mining.
Dual-core, multi-thread, and no I/O: It is equivalent to mining from two workers on both sides of the mountain.

Of course, some construction sites only allow one worker to work.

Although the example is far-fetched, you can give a rough explanation. One year later, I will analyze it to see if it can be further analyzed!

This article is transferred from www.35java.com

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.