Recently in-depth knowledge of multithreading, about multi-threading, we commonly used in the development of the thread pool to deal with a variety of business logic
For example, a large set of objects, want to quickly insert into the database, we can iterate, or we can use the thread pool in the same way to insert
Or a calling interface, each of which calls an interface over and over again, or you can use multithreading to invoke an interface
The following code can handle this problem
Package Com.demo;
Import java.util.LinkedList;
Import java.util.List;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.LinkedBlockingQueue;
Import Java.util.concurrent.ThreadPoolExecutor;
Import Java.util.concurrent.TimeUnit;
public class ThreadPool2 {
public static void Main (string[] args) {
/**
* Number of thread pools
*/
int corepoolsize = 10;
/**
* Maximum number of thread pools
*/
int maximumpoolsize = 100;
Executorservice ThreadPool = new Threadpoolexecutor (corepoolsize,maximumpoolsize, timeunit.seconds,new Linkedblockingqueue<runnable> ());
Executorservice ThreadPool =executors.newfixedthreadpool (4);
String dao = "DAO";
list<string> list = new linkedlist<string> ();
for (int i = 0; i < i++) {
List.add (i + "");
}
Task task = null;
for (String str:list) {
task = new Task (DAO, str);
Threadpool.execute (Task);
}
Threadpool.shutdown ();
}
}
Class Task implements Runnable {
/**
* Inject DAO
*/
Private String DAO;
/**
* Inject Beans
*/
Private String Bean;
Public Task (String dao, string bean) {
This.dao = DAO;
This.bean = Bean;
}
public void Run () {
try {
TimeUnit.SECONDS.sleep (1);
SYSTEM.OUT.PRINTLN ("Thread Name:" +thread.currentthread () + "--thread activation number-" +thread.activecount () + "---");
SYSTEM.OUT.PRINTLN (dao + "--insert into the database---" + bean);
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Threads-Multithreaded thread pool