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
- Public class Test {
- public static void Main (string[] args) {
- //TODO auto-generated method stub
- Testthread TT = new Testthread ();
- Tt.run ();
- }
- }
- Class Testthread extends Thread {
- static int i = 0;
- final static int max_i = 10;
- @Override
- public Void Run () {
- //TODO auto-generated method stub
- While (I < max_i) {
- System.out.println (i++);
- }
- }
- }
The results of the operation are as follows:
[Java]View PlainCopy
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 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
- Public class Test {
- public static void Main (string[] args) {
- //TODO auto-generated method stub
- Testthread TT = new Testthread ();
- Tt.run ();
- System.out.println ("Printed by main thread");
- }
- }
- Class Testthread extends Thread {
- static int i = 0;
- final static int max_i = 10;
- @Override
- public Void Run () {
- //TODO auto-generated method stub
- While (I < max_i) {
- System.out.println (i++);
- }
- }
- }
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
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 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
- Public class Test {
- public static void Main (string[] args) {
- //TODO auto-generated method stub
- Testthread TT = new Testthread ();
- Tt.run ();
- System.out.println (Thread.CurrentThread (). GetName ());
- System.out.println ("Printed by main thread");
- }
- }
- Class Testthread extends Thread {
- static int i = 0;
- final static int max_i = 10;
- @Override
- public Void Run () {
- //TODO auto-generated method stub
- System.out.println (Thread.CurrentThread (). GetName ());
- While (I < max_i) {
- System.out.println (i++);
- }
- }
- }
This code prints the current thread name, respectively, in the main thread and our Testthread method, and runs the following result:
[XHTML]View PlainCopy
- Main
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- Main
- 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
- Public class Test {
- public static void Main (string[] args) {
- //TODO auto-generated method stub
- Testthread TT = new Testthread ();
- Tt.start ();
- System.out.println (Thread.CurrentThread (). GetName ());
- System.out.println ("Printed by main thread");
- }
- }
- Class Testthread extends Thread {
- static int i = 0;
- final static int max_i = 10;
- @Override
- public Void Run () {
- //TODO auto-generated method stub
- System.out.println (Thread.CurrentThread (). GetName ());
- While (I < max_i) {
- System.out.println (i++);
- }
- }
- }
The results of the operation are as follows:
[XHTML]View PlainCopy
- Main
- Thread-0
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- Printed by main thread
- 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