Here's a brief explanation of the differences between the two parameters, followed by some additional instructions for the Java thread:1) Start:
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:
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. Summary: Call the Start method to start the thread, and the Run method is just a normal method call to thread, or execute in the main thread. Both methods should be familiar, put code that needs to be processed in parallel in the run () method, and the start () method will automatically call the run () method, which is prescribed by the JVM's memory mechanism. and the run () method must be public access and the return value type is void. Here are some additional instructions for the Java thread:
Java threads are implemented through the Java.lang.Thread class. When the VM starts, it has a thread defined by the main method. You can create a new thread by creating an instance of the thread. Each thread completes its operation by means of the method run () that corresponds to a particular thread object, and the method run () is called the thread body. Start a thread by calling the start () method of the thread class.
in Java, threads typically have five states of creation, readiness, operation, blocking, and death.
The first is the creation state. In the build thread object, there is no call to the object's Start method, which is the thread in the created state.
The second is the ready state. When the start method of the thread object is called, the thread enters the ready state, but at this point the thread dispatcher has not set the thread to the current thread and is in a ready state. After a thread is running, it is ready to return from waiting or sleeping.
The third is the running state. The thread dispatcher sets the thread in the ready state to the current thread, and the thread enters the running state and starts running the code in the Run function.
The four is the blocking state. A thread is paused when it is running, usually in order to wait for a certain time to occur (for example, a resource is ready) before it continues to run. Methods such as sleep,suspend,wait can cause thread blocking.
Five is the state of death. If a thread's Run method finishes executing or calls the Stop method, the thread dies. For a thread that has died, the start method can no longer be used to get it ready.
There are two ways of implementing and starting a thread
1. Write a class that inherits from the thread class, overriding the Run method. Start a thread with the Start method
2, write a class to implement the Runnable interface, the implementation of the Run method. Start with the new Thread (Runnable target). Start () method
Multithreading principle: the equivalent of playing a game machine, only a game console (CPU), but there are a lot of people to play, so, start is queued! When the CPU is selected you are your turn, you run (), and when the CPU runs out of time slices, the thread continues to queue, waiting for the next run ().
when Start () is called, the thread is placed in the waiting queue, waiting for the CPU to dispatch, not necessarily starting immediately, but putting the thread into a movable state. Then through the JVM, thread threads invokes the run () method, executing the thread body of this thread. Call run after start, so troublesome, in order not to call run directly? is to realize the advantages of multithreading, not this start.
1. Start() method to start the thread and really implement multi-threaded operation. instead of waiting for the Run method body code to complete, you can proceed directly to the following code, starting a thread by calling the start () method of the thread class, which is in the ready state and not running. The thread class then calls the method Run () to complete its operation, where the method run () is called the thread body, it contains the contents of the thread to be executed, and the Run method ends, and it terminates. The CPU then dispatches other threads.
2.the Run () method is called as a normal method. The program is still executed sequentially, waiting for the Run method body to complete before the execution of the following code can continue, only the main thread-this one, the program execution path is only one, so that does not achieve the purpose of writing threads.
Remember: multithreading is the use of time-sharing CPU, macro on all threads to execute together, also called Concurrency .
Public classTest { Public Static voidMain (string[] args) {Runner1 runner1=NewRunner1 (); Runner2 Runner2=NewRunner2 (); //thread (Runnable target) assigns a new thread object. Thread Thread1 =NewThread (Runner1); Thread thread2=NewThread (RUNNER2); //Thread1.start (); //Thread2.start (); Thread1.run (); Thread2.run (); } } classRunner1ImplementsRunnable {//implementing the Runnable interface, the JDK knows that this class is a thread Public voidrun () { for(inti = 0; I < 100; i++) {System.out.println ("Enter Runner1 Run status ——————————" +i); } } } classRunner2ImplementsRunnable {//implementing the Runnable interface, the JDK knows that this class is a thread Public voidrun () { for(inti = 0; I < 100; i++) {System.out.println ("Enter Runner2 running status ==========" +i); } } }
The difference between the run () and start () of Java Thread