1.start () method to start a thread, which really implements multithreading, without waiting for the Run method body code to complete and proceed directly to the following code:
Start a thread by invoking the start () method of the thread class.
This thread is in the ready state,
And not run.
This thread class then invokes method run () to complete its operation.
Here the method run () is called the thread body,
It contains the contents of this thread to be executed,
The Run method runs the end,
This thread terminates,
While the CPU is running another thread,
The 2.run () method is invoked as a normal method, whether the program will execute sequentially or wait until the Run method body completes execution before proceeding to the following code:
And if you use the Run method directly,
It's just a call to a method,
There's still only the main thread in the program--this one,
Its program execution path is still only one line,
This does not achieve the purpose of writing threads.
Multithreading via implements Runnable interface
Sample code:
The code is as follows |
Copy Code |
Package com; public class Testrunnable implements runnable{ private int num = 0; public static void Main (string[] args) { for (int i=0; i<5; i++) { New Thread (New testrunnable (i)). Start (); } } public testrunnable (int num) { This.num = num; } Implementation methods public void Run () { System.out.println ("Thread:" +this.num); } } |
The results printed in the previous example are unordered:
1 4 3 2 0
It could also be in another order, call start through the thread class, and actually implement multithreading without waiting for the run method to complete before starting a new thread.
If you would
The code is as follows |
Copy Code |
New Thread (New testrunnable (i)). Start () Change into New Thread (New testrunnable (i)). Run (), |
The printed result is in the order of:
0 1 2 3 4
Calling the Run method directly through the thread class is actually calling the run method of the Testrunnable class to view the run method of the thread class:
The code is as follows |
Copy Code |
public void Run () { if (target!= null) { Target.run (); } }
|
The thread class run method was found to invoke the testrunnable run (), so the
The code is as follows |
Copy Code |
New Thread (New testrunnable (i)). Run () |
is not a multithreaded, it executes a thread or is a primary thread.
The thread class's Start method is then posted and compared to the run method of the thread class above:
The code is as follows |
Copy Code |
/** * Causes this thread to begin execution; The Java Virtual Machine * Calls the <code>run</code> method of this thread. * <p> * The result was that two threads are running concurrently:the * Current thread (which returns the * <code>start</code> method) and the other thread (which executes its * <code>run</code> method). * <p> * It is never legal to start a thread more than once. * In particular, a thread may is restarted once it has completed * Execution. * * @exception Illegalthreadstateexception If the thread was already * started. * @see #run () * @see #stop () */ Public synchronized void Start () { /** * This method isn't invoked for the main method thread or "system" * Group Threads Created/set up by the VM. Any new functionality added * To the, the future may have to also is added to the VM. * * A Zero status value corresponds to state "NEW". */ if (threadstatus!= 0) throw new Illegalthreadstateexception (); /* Notify The group This thread are about to be started * So, it can be added to the group ' s list of threads * and the group ' s unstarted count can be decremented. */ Group.add (this); Boolean started = false; try { Start0 (); started = true; finally { try { if (!started) { Group.threadstartfailed (this); } catch (Throwable ignore) { /* do nothing. If Start0 threw a throwable then It is passed up the call stack * * } } } |