In Java, multithreading is widely used. In practical applications, a good software design does not recommend manual creation and destruction of threads. The creation and destruction of threads is very CPU-and memory-intensive because it requires the involvement of the JVM and the operating system. To do this, we typically use thread pooling in the face of multithreaded problems. In general, each thread pool consists of these modules: a task queue, a collection of worker threads, a thread factory, and the metadata for managing thread state.
The thread pool solves two problems: one is that by reducing the overhead of each task invocation, they typically provide enhanced performance when performing a large number of asynchronous tasks, and can also provide a way to bind and manage resources, including the threads that are used to execute the task set, and the second is to each threadpoolexecutor It also maintains some basic statistics, such as the number of tasks completed.
Thread pools are located in the Java.util.concurrent package.
The inheritance relationship of the Threadpoolexecutor class is:
public class Threadpoolexecutor extends Abstractexecutorservice
Among them, the inheritance relationship of abstract class Abstractexecutorservice is:
Public abstract class Abstractexecutorservice extends Object implements Executorservice
The inheritance relationship of the Executorservice interface is:
Public interface Executorservice extends Executor
Executor is a top-level interface.
In practical applications, we can use the Executors factory Method Executors.newcachedthreadpool () (No limit pool, can be automated Thread recovery), Executors.newfixedthreadpool (int) (fixed-size thread pool), or Executors.newsinglethreadexecutor () (a single background thread) to generate the thread pool, the thread pools generated by these methods have predefined settings for most usage scenarios.
The inheritance of executors is:
public class Executors extends Object
Since creating a child thread can inherit the thread class, implement the Runnable interface, and implement the callable interface in three ways, I will first create the thread in these three ways and then run through the line pool through a test class.
The code in Mythread.java is as follows:
public class MyThread extends thread{Public MyThread (String name) { Super (name);}int num=10;@Overridepublic void Run () { while (num>=0) { try { Thread.Sleep (100); } catch (Interruptedexception e) { E.printstacktrace (); } System.out.println (GetName () + "being number:" +num); num--; }}}The code in Myrun.java is as follows:
public class Myrun implements Runnable {Public Myrun (String name) { Thread.CurrentThread (). SetName (name);}int num=10;@Overridepublic void Run () { while (num>=0) { try { Thread.Sleep (100); } catch (Interruptedexception e) { E.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName () + "being counted:" +num); num--; }}}
The code in Mycall.java is as follows:
public class Mycall implements callable<string> {int num=10; @Overridepublic String call () throws Exception {while (num>=0) {try { Thread.Sleep (100); } catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "being counted:" +num); num--; }return Thread.CurrentThread (). GetName () + "smooth Execution"; }}
The code in the test file Test.java is as follows:
Import Java.util.concurrent.executionexception;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.future;import Java.util.concurrent.ThreadPoolExecutor ;p Ublic class Test {Create a thread pool with the Newfixedthreadpool () method, set up 2 threadsStatic Threadpoolexecutor pool= (Threadpoolexecutor) Executors.newfixedthreadpool (2);or generate thread connection pools in the following ways//Static Executorservice Pool=executors.newfixedthreadpool (2);public static void Main (string[] args) { future<string> S1 = pool.submit (New Mycall ()); future<string> s2 = pool.submit (New Mycall ()); try { System.out.println (S1.get ()); System.out.println (S2.get ()); } catch (Interruptedexception e) { E.printstacktrace (); } catch (Executionexception e) { E.printstacktrace (); } Remember: The thread pool is not terminated at the end of the threads execution, but manually terminated Pool.shutdown ();}public static void Main2 (string[] args) { Pool.submit (New Myrun ("Zhang San")); Pool.submit (New Myrun ("John Doe")); Pool.submit (New Myrun ("Harry")); Pool.submit (New Myrun ("Zhao Liu")); Remember: The thread pool is not terminated at the end of the threads execution, but manually terminated Pool.shutdown ();}public static void Main1 (string[] args) { Pool.submit (New MyThread ("Zhang San")); Pool.submit (New MyThread ("John Doe")); Pool.submit (New MyThread ("Harry")); Pool.submit (New MyThread ("Zhao Liu")); Remember: The thread pool is not terminated at the end of the threads execution, but manually terminated Pool.shutdown ();}}
Java Thread Pool Application