Discussion on multi-threaded output 1 to 100 million square

Source: Internet
Author: User

Problem: Print 1 to 100 million square results with multi-threaded knowledge output?

Problem teardown: 1. A large number of threads, consider the thread pool to manage

2. How does the main thread monitor other threads? (Print end time after all threads are finished)

3. Optimal number of threads

4.1 to 100 million how to divide the range

5. How to bring efficiency to the highest and spend time to a minimum

Knowledge Reserve: Know Jdk7 increase fork (big problem cut into small enough problem) join (Result summary of small problem) framework, learn forkjoin in detail, find the perfect solution to solve 1-4 problems?

About Forkjoin's support please Baidu, no longer in this statement.


Try:

Take 0~1000, valve value 1000 as an example

Parallel number Time (MS)

Main thread only 41104

1 39710

2 40299

3 40771

4 40499

5 41380

6 40089

7 40080

8 41940

9 43411

10 42245

11 40670

12 41621

13 41075

16 40063

100 42164

Question: Is it necessary to calculate the optimal number of processes? How to calculate? The difference is not very obvious ah? Confused

Take 0~1000, parallel number 16 as an example

Threshold Time (MS)

100 43371

1000 42639

10000 42750

100000 42535

For a long time, the feeling is not very different, the summary takes 6, 7 minutes


The source of the final discovery: System.out.prinln () is too time consuming, using StringBuilder replacement, 40s perfect output

The final code is assigned:

Forkjoin Example: Core approach

Sum to verify that each value is obtained, as follows:

The core output method is as follows:



The summation method is used to verify that the checksum data is left behind and to ensure the correctness of the output result. Replace Intermediate variables with method


Use the JDK's dynamic agent to print the time difference

Call to Forkjoin, set the start value, end value and threshold, call the generated queue to the thread pool to digest, and finally close the thread pool

The final Main method:

Results of the final run:

After using StringBuilder:

Take 0~1000, valve value 1000 as an example

Parallel number Time (MS)

16 4268

10 times times the time difference, haha.


End result: 1.1 billion output: 39575ms, i.e. 40 seconds


Problem thinking: multithreading must be faster than a single thread? What are the business scenarios in which multithreading is considered?


Discussion on multi-threaded output 1 to 100 million square

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.