Differences between Java start and run startup threads

Source: Internet
Author: User
Tags stub

We know that we start a thread by invoking the Start method of the thread, so can we call the Run method directly to start a thread?

Let's look at the following code:

[Java]View PlainCopy
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. //TODO auto-generated method stub
  4. Testthread TT = new Testthread ();
  5. Tt.run ();
  6. }
  7. }
  8. Class Testthread extends Thread {
  9. static int i = 0;
  10. final static int max_i = 10;
  11. @Override
  12. public Void Run () {
  13. //TODO auto-generated method stub
  14. While (I < max_i) {
  15. System.out.println (i++);
  16. }
  17. }
  18. }

The results of the operation are as follows:

[Java]View PlainCopy
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
    7. 6
    8. 7
    9. 8
    10. 9

Perhaps someone will come to the conclusion that it is possible to start a thread so that we can modify the program a little bit, and you will find a problem:

[Java]View PlainCopy
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. //TODO auto-generated method stub
  4. Testthread TT = new Testthread ();
  5. Tt.run ();
  6. System.out.println ("Printed by main thread");
  7. }
  8. }
  9. Class Testthread extends Thread {
  10. static int i = 0;
  11. final static int max_i = 10;
  12. @Override
  13. public Void Run () {
  14. //TODO auto-generated method stub
  15. While (I < max_i) {
  16. System.out.println (i++);
  17. }
  18. }
  19. }

Here, only one line of code is added to the main thread, and a line of "printed by main thread" is printed, and the result is as follows:

[XHTML]View PlainCopy
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
    7. 6
    8. 7
    9. 8
    10. 9
    11. Printed by main thread

Skilled multithreaded development to find the problem, why "printed by main thread" will be printed in the last line? Does the Testthread class hold the time period?

Our analysis of the above code, in fact very simple, this is just a common class in the method call, is actually a single-threaded execution, we have to modify the code to further verify this point:

[Java]View PlainCopy
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. //TODO auto-generated method stub
  4. Testthread TT = new Testthread ();
  5. Tt.run ();
  6. System.out.println (Thread.CurrentThread (). GetName ());
  7. System.out.println ("Printed by main thread");
  8. }
  9. }
  10. Class Testthread extends Thread {
  11. static int i = 0;
  12. final static int max_i = 10;
  13. @Override
  14. public Void Run () {
  15. //TODO auto-generated method stub
  16. System.out.println (Thread.CurrentThread (). GetName ());
  17. While (I < max_i) {
  18. System.out.println (i++);
  19. }
  20. }
  21. }

This code prints the current thread name, respectively, in the main thread and our Testthread method, and runs the following result:

[XHTML]View PlainCopy
    1. Main
    2. 0
    3. 1
    4. 2
    5. 3
    6. 4
    7. 5
    8. 6
    9. 7
    10. 8
    11. 9
    12. Main
    13. Printed by main thread

The same thread is running in the Testthread class and the main thread, stating that it is not possible to use multithreading when calling run directly, then change the call of the above run method to the transfer of the Start method and look at:

[Java]View PlainCopy
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. //TODO auto-generated method stub
  4. Testthread TT = new Testthread ();
  5. Tt.start ();
  6. System.out.println (Thread.CurrentThread (). GetName ());
  7. System.out.println ("Printed by main thread");
  8. }
  9. }
  10. Class Testthread extends Thread {
  11. static int i = 0;
  12. final static int max_i = 10;
  13. @Override
  14. public Void Run () {
  15. //TODO auto-generated method stub
  16. System.out.println (Thread.CurrentThread (). GetName ());
  17. While (I < max_i) {
  18. System.out.println (i++);
  19. }
  20. }
  21. }

The results of the operation are as follows:

[XHTML]View PlainCopy
    1. Main
    2. Thread-0
    3. 0
    4. 1
    5. 2
    6. 3
    7. 4
    8. 5
    9. 6
    10. 7
    11. 8
    12. Printed by main thread
    13. 9

Obviously, this is what we want to see, so the conclusion is that only the thread's Start method is called, which is controlled by the JVM to produce multi-threading, and the direct call to the Run method is just a common single-regulation regulation.

Differences between Java start and run startup threads

Related Article

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.