An overview
Only one process runs at least one thread, and in Java there is a thread that is created by the JVM when the main method is called.
1 Package link.summer7c.test; 2 3 Public class test{4 Public Static void Main (string[] args) {5 System.out.println (Thread.CurrentThread (). GetName ()); 6 }7 }
Run Result: Main
The thread called main is executing the code in the main () method. But the main and main methods do not matter, just because the name is similar.
Implementation of two Java multi-threading
Java in the JDK, there are two ways to implement multithreading, one is to inherit the thread class, and the other is to implement the Runnable interface.
Inherit the thread method:
1 Packagelink.summer7c.test;2 3 Public classTestextendsthread{4 Public voidrun () {5 Super. Run ();6System.out.println ("The test!");7 }8 Public Static voidMain (string[] args) {9Test test=NewTest ();Ten Test.start (); One System.out.println (Thread.CurrentThread (). GetName ()); A } -}
Operation Result:
Main
The test!
According to the procedure, the Test should be output first. In the output main, but the result is not so this means that it is not related to the order of running the results and code when running multithreading.
Therefore, the thread is a subtask, and the CPU is executed in an indeterminate manner.
Then look at the source code for thread:
1 Public class Implements Runnable
Note that the thread class implements the Runnable class, and they have polymorphic relationships.
To verify that the randomness of a thread can run two threads while looping the output, you will find that two threads are not executed sequentially:
1 Packagelink.summer7c.test;2 3 Public classtest{4 Public Static voidMain (string[] args) {5MyThread mythread=NewMyThread ();6Mythread.setname ("Mythreadrun");7 Mythread.start ();8 for(inti=0;i<10;i++){9System.out.println ("main=" +Thread.CurrentThread (). GetName ());Ten } One } A } - - classMyThreadextendsthread{ the Public voidrun () { - for(inti=0;i<10;i++){ -System.out.println ("run=" +Thread.CurrentThread (). GetName ()); - } + } -}
Results:
main=mainrun =mythreadrunrun =< Span style= "color: #000000;" >mythreadrunmain =mainrun = Mythreadrunmain =mainrun = Mythreadrunmain =mainrun = Mythreadrunmain =mainrun = Mythreadrunmain =mainrun = Mythreadrunmain =mainrun = Mythreadrunmain =mainrun = Mythreadrunmain =mainrun = Mythreadrunmain =main
The Start method is not the method to start the program, and all start is executed concurrently.
Implement the Runnable interface:
1 Packagelink.summer7c.test;2 3 Public classtest{4 Public Static voidMain (string[] args) {5Runnable mythread=NewMyThread ();6Thread thethread=NewThread (myThread);7 Thethread.start ();8System.out.println ("Main run!");9 }Ten } One A classMyThreadImplementsrunnable{ - @Override - Public voidrun () { theSYSTEM.OUT.PRINTLN ("Thread run! "); - } - -}
From here can be seen, the implementation of the Runnable interface or a new thread object, here I am very puzzled, since runnable is a multi-threaded interface Why there is no Start method, then I read the source: public
Interface Runnable {
public abstract void run ();
}
Originally runnable interface does not have start, so the method of start is written in thread, so I looked at the source code of thread, found the Start method.
1 Start0 ();
The Start0 method is called in the Strat method, which is written in this way:
Private native void Start0 ();
As can be seen, this method has been involved in the interface of the operating system, it is likely to be written in C/s, this is not explored in depth.
Java Multithreaded Learning notes (i)