Recently saw a topic, the code is as follows:
Copy Code code as follows:
public static void Main (String args[]) {
Thread t = new Thread () {
public void Run () {
Pong ();
}
};
T.run ();
System.out.println ("ping");
}
static void Pong () {
System.out.println ("Pong");
}
Q, what will the results output?
I ran it many times and the result was Pong ping. Finally, the key point is found, thread object T, the call is not the start () method, but the run () method. I later opened the breakpoint mode debug, and found that the call to the run () method, the entire program has only one thread, and call the Start () method, the program will be more than one thread. At this point and the main thread contention CPU, there may be a variety of results, but because the following output method is quickly implemented, so basically is "ping pong" output.
So the difference between run () and start () is:
Run () is a method defined in the Runnable interface to allow client programmers to write their own functional code in this method. There is no difference between a direct call and a normal class calling your own member methods.
The start () is the flag that the thread starts to run, and when this method is invoked, a separate thread is added to the program, and then the run () method is executed.
So I think, I want to write a separate thread, it is best to inherit thread to do, if it is to implement the interface, to the main thread, or to new thread (new Yourrunnableclass ()) to call, it is very inconvenient.