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
1 Public classTest {2 Public Static voidMain (string[] args) {3Runner1 Runner1 =NewRunner1 (); 4Runner2 Runner2 =NewRunner2 (); 5 //thread (Runnable target) assigns a new thread object. 6Thread Thread1 =NewThread (Runner1); 7Thread thread2 =NewThread (RUNNER2); 8 //Thread1.start (); 9 //Thread2.start (); Ten Thread1.run (); One Thread2.run (); A } - } - the 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); + } - } + } A at 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 Run status ==========" +i); - } - } in}
Above finishing from: http://blog.csdn.net/xuxurui007/article/details/7685076
"Steps to Create and start a thread ( implement runnable interface mode )"
1. define the implementation class for the Runnable interface and override the Run method in it. the method body of the run () method is the thread execution body .
Class Sonthread Implement runnable{
public void Run () {
......
}
}
2. Create an instance of the Runnable interface implementation class. Sonthread s1=new Sonthread ();
3. Use this instance as the target of the thread to create the thread object. Thread T1 =new thread (S1);
4. call the object's start () method to start the thread. T1.start ();
"Note one: About naming"
You can specify a name for the thread object when you create the thread object
"Note two: How to construct Thread"
The Runnable object as the Target,runnable implementation class for the thread object contains the Run method only as an executor. That is, the function of the thread class is to wrap the run method into the thread's execution.
The thread object that is actually running is still a thread instance, except that the thread thread is responsible for executing its target's Run method.
1 //1.1 Defining an implementation class for the Runnable interface2 classSecondthreadImplementsrunnable{3 4 //1.2 Overriding the Run method5 @Override6 Public voidrun () {7 for(inti=0;i<10;i++){8System.out.println (Thread.CurrentThread (). GetName () + "= =" +i);9 }Ten } One A } - Public classDemo2 { - the Public Static voidMain (string[] args) { - //2. Creating an instance of the Runnable interface implementation class -Secondthread s1=NewSecondthread (); -Secondthread s2=NewSecondthread (); + //2. Use the Runnable interface to implement an instance of the class as the target of thread, creating a Thread object -Thread t1=NewThread (S1); +Thread t2=NewThread (S2, "higgin");//you can name a thread object while creating it A at //Start Thread - T1.start (); - T2.start (); - - for(inti=0;i<10;i++){ -System.out.println (Thread.CurrentThread (). GetName () + "= =" +i); in } - } to}
Above finishing from: http://www.cnblogs.com/HigginCui/p/5901593.html
Parse thread (runable target ...) via JDK source code. Which run method is called
Code Listing 1:
1 NewThread (NewRunnable () {2 @Override3 Public voidrun () {4System.out.println ("Run of Runnable"); 5 } 6 }) { 7 Public voidrun () {8System.out.println ("Run of Thread"); 9 } Ten}.start ();
Code Listing 2:
1 NewThread (NewRunnable () {2 @Override3 Public voidrun () {4System.out.println ("Run of Runnable"); 5 } 6 }) { 7 Public voidrun () {8System.out.println ("Run of Thread"); 9 Super. Run (); Ten } One}.start ();
First, let's look at the thread source of the JDK, fragment 3 is as follows:
1 Private Runnable Target;
Public void run () { ifnull) { target.run (); } }
In the Run () method, first the target is checked for null, and if not, the run () method of the target is executed.
So, for the execution of the above two pieces of code, it is clear.
In the first code Snippet 1, the Run () method of the thread was rewritten, and a Runnable object was passed in, and the object also implemented the run () method. After the thread object calls the start () method, the run () method that the object overrides is executed, and the output is the run of thread, and when the output is finished, the run () method returns, and the life cycle of the thread object ends.
In the second code Snippet 2, the Run () method of the thread is also rewritten, and a Runnable object is passed in, implementing the Run () method. The only difference is that, in the run method of the thread rewrite, Super.run () is executed after the printout, which is interesting.
First, after the thread starts running, it executes its overridden run () method, outputting the run of thread.
Next Call Super.run (), which is called the Run () method of the superclass, and the Run () method of the superclass, which is the run () of the thread class defined by the JDK, is executed as shown in code snippet 3 above; it is clear that target is not empty, and the run () method of the object is called , the run of Runnable is output.
If the thread above does not override the run () method, the result is the same. The thread's run () method is executed first, because the method is not overridden at this time, so the run () method of the JDK is called, that is, the code snippet 3 above, in which the target is determined to be empty, obviously not, Therefore, the run () method that implements the Runnable object is called.
Summary: For thread (Runnable target ...), the thread's own run () method is executed first, regardless of whether the incoming target is empty or not. If the method is overridden and there is no Super.run () in the method, then the run () method of the runnable implementation is never called, and if the method is not overridden, the target is determined to be null, which determines the run () method that invokes the target implementation If the method is overridden and there is Super.run () in the method, after all the code before the statement is executed, the target is determined to be empty, which determines the run () method that invokes the target implementation, and then executes the code after the statement executes.
Above finishing from: http://blog.csdn.net/guguituzi/article/details/44593863
Two methods of thread initiation, runnable interface, Call of Run ()