This example shares the implementation code of Java handwritten thread pool for your reference. The specific content is as follows
1. Thread pool is a form of multi-threading. During the processing, tasks are added to the queue, and then these tasks are automatically started after the thread is created. Thread pool threads are all background threads.
2. Simple thread pool architecture
3. Simple thread pool code (self-optimization)
import java.util.List;/** * Thread interface* * @Author yjian * @Date 14:49 2017/10/14 **/public interface IThreadPool { //Add task void execute(Runnable task); //Add task void execute(Runnable[] tasks); //Add task void execute(List<Runnable> tasks); //Destroy thread void destroy();} import java.util.LinkedList;import java.util.List;import java.util.concurrent.atomic.AtomicLong;/** * Thread implementation class (simple implementation, self-optimization. Provide ideas) * * @Author yjian * @Date 14:49 2017/10/14 **/@SuppressWarnings("ALL")public class ThreadPoolImpl implements IThreadPool { //The number of threads is enabled by default static int WORKER_NUMBER = 5; //The number of threads completed is static volatile int sumCount = 0; //Task queue list is not thread-safe, and can be optimized to BlockingQueue static List<Runnable> taskQueue = new LinkedList<Runnable>(); //Thread Workgroup WorkerThread[] workThreads; //Atomic static AtomicLong threadNum = new AtomicLong(); static ThreadPoolImpl threadPool; //Construction method public ThreadPoolImpl() { this(WORKER_NUMBER); } public ThreadPoolImpl(int workerNum) { this.WORKER_NUMBER = workerNum; //Develop worker thread space workThreads = new WorkerThread[WORKER_NUMBER]; //Start create worker thread for (int i = 0; i < WORKER_NUMBER; i++) { workThreads[i] = new WorkerThread(); Thread thread = new Thread(workThreads[i], "ThreadPool-worker" + threadNum.incrementAndGet()); System.out.println("Initialize number of threads" + (i + 1) + "-----------Current thread name:" + thread.getName()); thread.start(); } } @Override public String toString() { return "Number of worker threads is" + WORKER_NUMBER + "Number of completed tasks" + sumCount + "Number of waiting tasks" + taskQueue.size(); } //Get thread pool public static IThreadPool getThreadPool() { return getThreadPool(WORKER_NUMBER); } public static IThreadPool getThreadPool(int workerNum) { //Fault tolerance, if less than or equal to 0, the default number of threads is if (workerNum <= 0) { workerNum = WORKER_NUMBER; } if (threadPool == null) { threadPool = new ThreadPoolImpl(workerNum); } return threadPool; } @Override public void execute(Runnable task) { synchronized (taskQueue) { taskQueue.add(task); taskQueue.notifyAll(); } } @Override public void execute(Runnable[] tasks) { synchronized (taskQueue) { for (Runnable task : tasks) { taskQueue.add(task); } taskQueue.notifyAll(); } } @Override public void execute(List<Runnable> tasks) { synchronized (taskQueue) { for (Runnable task : tasks) { taskQueue.add(task); } taskQueue.notifyAll(); } } @Override public void destroy() { //Where is a task still present in the loop, if there is a waiting 20 milliseconds of processing time while (!taskQueue.isEmpty()) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } //If the task queue has been processed, destroy the thread and clear the task for (int i = 0; i < WORKER_NUMBER; i++) { workThreads[i].setWorkerFlag(); workThreads[i] = null; } threadPool = null; taskQueue.clear(); } //Create worker thread pool class WorkerThread extends Thread { //Use to identify the current thread belongs to the active available state private boolean isRunning = true; @Override public void run() { Runnable runnable = null; //Never loop while (isRunning) { //Non-thread-safe, so synchronized (taskQueue) { while (isRunning && taskQueue.isEmpty()) { try { //If the task queue is empty, wait for the 20 millisecond listening task to reach taskQueue.wait(20); } catch (Exception e) { e.printStackTrace(); } } //The task queue is not empty if (!taskQueue.isEmpty()) { runnable = taskQueue.remove(0);//Get the first task} } if (runnable != null) { runnable.run(); } sumCount++; runnable = null; } } //Destroy thread public void setWorkerFlag() { isRunning = false; } }} import java.util.ArrayList;import java.util.List;/** * Test class* * @Author yjian * @Date 15:37 2017/10/14 **/public class ThreadPoolTest { public static void main(String[] args) { //Get thread pool IThreadPool t = ThreadPoolImpl.getThreadPool(20); List<Runnable> taskList = new ArrayList<Runnable>(); for (int i = 0; i < 100; i++) { taskList.add(new Task()); } //Execute task t.execute(taskList); System.out.println(t); //Destroy thread t.destroy(); System.out.println(t); } static class Task implements Runnable { private static volatile int i = 1; @Override public void run() { System.out.println("Currently processed thread:" + Thread.currentThread().getName() + "Execute task" + (i++) + "Complete"); } }}After studying the spring source code, carefully check which spring commonly used patterns are used by the code. The specifications for writing programs should be the same as spring.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.