public class Threadmodle {public static void main (string[] args) throws Interruptedexception {thread t = new Thread (New Ru Nnable () {@Overridepublic void run () {System.out.println ("Run ThreadID:" + thread.currentthread (). GetId ());}); System.out.println ("Main ThreadID:" + thread.currentthread (). GetId ());//t.run (); T.start (); T.join ();}}
1. Start ():
Let's take a look at the introduction of this method in the API:
Causes the thread to start executing, and the Java virtual machine invokes the thread's run
method.
The result is two threads running concurrently, when the front thread (returned from the call to the start
method) and another thread (executing its run
method).
It is illegal to start a thread multiple times. This is especially the time when the thread has finished executing and cannot be restarted.
Start the thread with the Start method, and actually implement multi-threaded operation, without waiting for the Run method body code to complete and proceed directly to execute the following code. By invoking the start () method of the thread class to start a thread, the thread is in a ready (operational) state and is not running, and once the CPU time slice is taken, the run () method is started, where the method run () is called the thread body, which contains the contents of the thread to be executed. This thread terminates when the Run method finishes running.
2. Run ():
Let's take a look at the introduction of the method in the API first:
If the thread is constructed using a stand-alone Runnable
running object, the method of the object is called, Runnable
run
Otherwise the method does nothing and returns.
Thread
Subclasses should override this method.
The run () method is just a common method of the class, if you call the Run method directly, the program is still only the main thread of the threads, its program execution path is only one, or to execute sequentially, or to wait for the Run method body execution before you can continue to execute the following code, This will not achieve the purpose of writing threads.
Executing the above example, the run and start methods are executed separately, and you can see that the Run method runs the same thread ID as the main
Java Thread class run and start method differences