Total number of threads required for computing programs-from Java Virtual Machine concurrent programming

Source: Internet
Author: User

Total number of threads required by the computing program:
Number of threads = number of available CPU cores/(1-blocking factor)

The blocking coefficient is between 0 and 1.

The blocking coefficient of computing-intensive tasks is 0, while that of IO-intensive tasks is close to 1. A completely congested task is doomed to fail, so we don't have to worry that the blocking coefficient will reach 1.

To better determine the number of threads required by the program, we need to know the following two key parameters:
1. Number of available processors

2. Task blocking Coefficient

The first parameter is easy to determine (runtime. getruntime (). availableprocessors ()).

The blocking factor can be determined by using some performance analysis tools or Java. Lang. managenment API to determine the ratio of the time consumed by threads in system I/O operations to the time consumed by CPU intensive tasks.

Demo:

// Test formula: Number of threads = number of available CPU cores/(1-blocking factor) @ testpublic void testinvokeall () throws interruptedexception, executionexception {// return the number of available processors to the Java Virtual Machine (my current machine is a I3 dual-core four-thread, and the returned value is 4) int numberofcores = runtime. getruntime (). availableprocessors (); double blockingcoefficient = 0; int poolsize = (INT) (numberofcores/(1-blockingcoefficient); system. out. println ("poolsize:" + poolsize); // create a thread pool that can reuse a fixed number of threads and run these threads in a shared unbounded queue mode: executorservice services = executors. newfixedthreadpool (poolsize); List <callable <string> tasks = new calllist <callable <string> (); For (INT I = 10; I> = 1; I --) {final string restr = "current call" + I; Final Int J = I; tasks. add (New callable <string> () {@ overridepublic string call () throws exception {// simulate timeunit blocking. seconds. sleep (j); Return restr ;}}) ;}long begintime = system. currenttimemillis (); List <future <string> invokevalues = services. invokeall (tasks); system. out. println ("time:" + (system. currenttimemillis ()-begintime); iterator <future <string> iter = invokevalues. iterator (); While (ITER. hasnext () {system. out. println (ITER. next (). get ();} services. shutdown ();}

When the blocking coefficient is set to 0, the consumed time is 15003, and the blocking coefficient is set to 0.9, the consumed time is 10001.

Poolsize: 4
Time: 15003
Current call 10
Current call 9
Current call 8
Current call 7
Current call 6
Current call 5
Current call 4
Current call 3
Current call 2
Current call 1

Poolsize: 40
Time: 10001
Current call 10
Current call 9
Current call 8
Current call 7
Current call 6
Current call 5
Current call 4
Current call 3
Current call 2
Current call 1

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.