Disclaimer: The concept of threading and the related concepts of the process, you can refer to other information on the network, this is only to discuss how multithreading is implemented.
First, the simple understanding of multithreading
Understand what is multi-threading, the little bit of popular understanding is: in a program, I want to have this program to complete multiple tasks.
For example, when the main function main is printing all the integers between 1~100, it requires that the main function print to 20, and then run the program in another class so that it prints all the integers between the 10~100.
Here ignores the same process of multithreading between the preemption time problem, the above example requirement is to require a program as long as there are two programs running at the same time, that is not allowed to appear no matter how many times the program runs the main function of the 1~100 all the integers are printed before printing 10~100 All integers between (PS: If this is the case, it is the method that main calls the other class, which is a single thread).
Second, the creation of multi-threaded three ways (niche writing is more complex and wordy, recommended reading, the reference code read again.) )
The first way:
Create: Write a Class MyThread1 (this is just a custom name for the niche, which is convenient to match the name in the code) Let it inherit the Thread class,
and put the program that requires multi-threaded operation into the public void run () method.
start: in the main function, new shows an instance of the MyThread1 class.
run: call the Start () method of the instance of the MyThread1 class.
The second way:
Create: Write a class MyThread2 let it implement the Runnable interface, and rewrite the Run () method (put a program that requires multiple threads into the public void Run () method).
start: in the main function, new shows an instance of the MyThread1 class,
New out of the thread class (constructed with target),
The instance of the MyThread1 class is passed into the constructor of the thread class as a parameter.
run: call the Start () method of the instance of the Thread class.
The Third Way:
Create: Implement the Callable interface (the niche defines this class as mycallable), and implement the Call () method, noting that the call () method has a return value.
Start:The implementation class mycallable for the new out callable interface,
New out of the Futuretask class instance task,
Put the return value of the call () method into the construction method of the Futuretask class,
Put the task into the new Thread construction method.
run: call the Start () method of the instance of the Thread class.
Three ways to summarize the pros and cons:
The first method benefits : Creating an instance of a class that already inherits the Thread class, calling the start () method to run, the code is easy to write.
Of course, the disadvantage is to inherit the thread parent class, there is no way to inherit other classes, extensibility is not good. Therefore, it is generally not recommended to create multithreading in this way.
The second method benefits : Inherits the interface, the extensibility is good.
The disadvantage is relative, the first way bad is relative to the second, and the second way, compared to the third Way, the run () method has no return value, and cannot declare the exception thrown.
The Third Way the benefits above have been explained, the bad is the coding is very troublesome (the niche is too stupid to see a long day to understand)
Four, three ways to create multi-threaded code demonstration
Description: The niche will rename each thread in every way, just to make it easier for the code to run the effect demo.
Java default thread name rule: The main function in Java itself is a default main thread, the name is: Main (in the following example can confirm the main threads in the JVM default name)
If you define a thread that does not have a defined name, the system defaults to Thread-0,thread-1, ... Named by the naming method. You can also rename a thread by constructing the method.
4.1 Inheriting the Thread class
Take a peek. The API discovers that the thread class implements the Runnable interface, CurrentThread () static method returns the instance of the current thread, and then GetName () gets the name of the current thread.
MyThread1 Threads:
1 PackageCom.test.threadDemo1;2 3 Public classMyThread1extendsThread {4 5 PublicMyThread1 (String name) {6 This. SetName (name);7 }8 9 Public voidrun () {Ten for(inti=20;i<=100;i++) { OneSystem.out.println (GetName () + "..." +i); A } - } -}
Main thread:
1 PackageCom.test.threadDemo1;2 /**3 * One of the implementations of multithreading: Inheriting the Thread class4 * @authorAdministrator5 *6 */7 Public classThreadDemo1 {8 Public Static voidMain (string[] args) {9 for(inti=1;i<=100;i++) {TenSystem.out.println (Thread.CurrentThread (). GetName () + "..." +i); One A if(i==30) { -MyThread1 MyThread =NewMyThread1 ("New Thread"); - Mythread.start (); the } - } - } -}
Running results: (there is a cross-running situation occurs, is multithreaded)
As you can see from the results, when the main thread runs to 30, it's still "preemption" the CPU.
4.2 Implementing the Runable interface
MyThread Threads:
1 PackageCom.test.threadDemo1;2 3 Public classMyThread2ImplementsRunnable {4 5 PublicMyThread2 (String name) {6 Thread.CurrentThread (). SetName (name);7 }8 9 @OverrideTen Public voidrun () { One for(inti=20;i<=100;i++) { ASystem.out.println (Thread.CurrentThread (). GetName () + "..." +i); - } - } the -}
Main thread:
1 PackageCom.test.threadDemo1;2 /**3 * Multithreading implementation of the second: implementation of the Runable interface4 * @authorAdministrator5 *6 */7 Public classThreadDemo2 {8 Public Static voidMain (string[] args) {9 for(inti=1;i<=100;i++) {TenSystem.out.println (Thread.CurrentThread (). GetName () + "..." +i); One A if(i==30) { -MyThread2 MyThread =NewMyThread2 ("New Thread"); - theThread thread =NewThread (myThread); - - Thread.Start (); - } + } - } +}
Operation Result:
4.3 Implementation of the callable interface and the future interface
The implementation class of the future interface is Futuretask, which implements the Runable interface, so it can be passed as the target parameter to the Thread class construction method.
FutureTask(Callable<V> callable)
From the construction method, we can see that the implementation class of the callable is passed into the Futuretask construction method.
MYTHREAD3 Threads:
1 PackageCom.test.threadDemo1;2 3 Importjava.util.concurrent.Callable;4 5 Public classMyThread3ImplementsCallable<integer> {6 7 @Override8 PublicInteger Call ()throwsException {9 inti = 20;Ten for(; i<=100;i++) { OneSystem.out.println (Thread.CurrentThread (). GetName () + "..." +i); A } - returni; - } the -}
Main thread:
1 PackageCom.test.threadDemo1;2 3 Importjava.util.concurrent.Callable;4 ImportJava.util.concurrent.FutureTask;5 6 /**7 * Multithreading implementation of the third: the implementation of the callable interface and the future interface8 * @authorAdministrator9 *Ten */ One Public classThreadDemo3 { A Public Static voidMain (string[] args) { - -Callable<integer> myThread3 =NewMyThread3 (); thefuturetask<integer> task =NewFuturetask<integer>(MYTHREAD3); - - for(inti=1;i<=100;i++) { -System.out.println (Thread.CurrentThread (). GetName () + "..." +i); + - if(i==30) { + AThread thread =NewThread (Task, "New Thread"); at Thread.Start (); - } - } - } -}
Operation Result:
"Java Multithreading" multi-threaded creation three ways--notes