Additional overhead for Java to start a new thread

Source: Internet
Author: User

When starting a new thread, there will be additional overhead for a certain period of time, so not all cases should use multithreading. The following code snippet is used for a simple experiment:

// TimeCostToStartNewThread. javapublic class TimeCostToStartNewThread {public static void main (String [] args) {long now1 = System. nanoTime (); for (int I = 0; I <10000000; I ++) {// I values:,} long current1 = System. nanoTime (); System. out. println ("current-now 1 =" + (current1-now1); long now2 = System. nanoTime (); new Thread (new Runnable () {public void run () {for (int I = 0; I <10000000; I ++ ){}}}). start (); long current2 = System. nanoTime (); System. out. println ("current-now 2 =" + (current2-now2 ));}}


D: \ CodingLearning \ Java multithreading design> javac TimeCostToStartNewThread. java
D: \ CodingLearning \ Java multithreading design> java TimeCostToStartNewThread
Current-now 1 = 906
Current-now 2 = 427110

D: \ CodingLearning \ Java multithreading design> javac TimeCostToStartNewThread. java
D: \ CodingLearning \ Java multithreading design> java TimeCostToStartNewThread
Current-now 1 = 4527
Current-now 2 = 435864

D: \ CodingLearning \ Java multithreading design> javac TimeCostToStartNewThread. java
D: \ CodingLearning \ Java multithreading design> java TimeCostToStartNewThread
Current-now 1 = 45277
Current-now 2 = 394511

D: \ CodingLearning \ Java multithreading design> javac TimeCostToStartNewThread. java
D: \ CodingLearning \ Java multithreading design> java TimeCostToStartNewThread
Current-now 1 = 419263
Current-now 2 = 456993

D: \ CodingLearning \ Java multithreading design> javac TimeCostToStartNewThread. java
D: \ CodingLearning \ Java multithreading design> java TimeCostToStartNewThread
Current-now 1 = 683679
Current-now 2 = 429525

D: \ CodingLearning \ Java multithreading design> javac TimeCostToStartNewThread. java
D: \ CodingLearning \ Java multithreading design> java TimeCostToStartNewThread
Current-now 1 = 3157601
Current-now 2 = 447938

D: \ CodingLearning \ Java multithreading design>

From the above results, we can know that the additional overhead for starting a new thread is about 4*(6 to the power of 10) nanoseconds. When the overhead time for using sequential programming is greater than this value, we can consider multithreading.

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.