Multithreading is a major feature of the Java language that distinguishes it from other languages. Other languages to C + + if you want to implement multithreading, you must invoke the multithreading mechanism of the operating system (I'm not sure I remember right
the interested in their own online check).
There are two ways to implement multithreading before Java5 (inheriting the thread class and implementing the Runnable interface)
Each of them is:
Use the new thread () and new Thread (Runnable) form
The first is to call the thread 's run method directly, so we tend to use the thread subclass, the new Subthread ().
The second kind of invocation
Runnable The run method.
The first type:
New Thread () {}.start (); This means that the run method of the thread subclass object is called, and new Thread () {} represents an instance object of an anonymous subclass of thread, with the following code for the subclass plus the Run method:
New Thread () {
public void Run () {
}
}.start ();
The second type:
New Thread (
New Runnable () {}
). Start ();
This means that the run method of the Runnable object accepted by the thread object is called, and new Runnable () {} represents an instance object of an Runnable anonymous subclass.
The code for the Runnable subclass plus the Run method is as follows:
New Thread (New Runnable () {
public void Run () {
}
}
). Start ();
Starting with Java5, there are a number of thread pools that create multithreading:
Package Com.yonyou.test;import java.util.concurrent.executorservice;import java.util.concurrent.executors;/** * Test class * @author Little Hao * @ Created date 2015-3-2 */public class Test{public static void Main (string[] args) {Executorservice pool = executors. Newfixedthreadpool (3); for (int. i=0;i<10;i++) {Pool.execute (new Runnable () {public void run () {System.out.println ()}});} Executors.newcachedthreadpool (). Execute (new Runnable () {public void Run () {}}); Executors.newsinglethreadexecutor (). Execute (new Runnable () {public void Run () {}});}
There are several ways to implement multithreading in Java and how to implement multithreading