java thread pool

Server when processing client requests, the client is often faced with the task of simple, single. If for each task, creates a thread of execution, then the task for thousands of clients, the server will create tens of thousands of threads. This will make the operating system thread context switching frequently, increasing the load on the system, a waste of system resources. Thread pool technology solves this problem by a number of pre-created threads. These threads handle with the task submitted by the client, to avoid the frequent thread creation and destruction overhead.

Here is a simple thread pool Interface Definition

public interface ThreadPool<Job extends Runnable> {
    // 执行一个Job,这个Job需要实现Runnable
    void execute(Job job);
    void shutdown();
    void addWorkers(int num);
    void removeWorkers(int num);
    int getJobSize();
}

The client can submit tasks to the thread pool by execute. Thread Pool provides a decrease / increase worker threads and methods to close thread. Here worker thread represents a recurring Job, and Job have each submitted by the client will enter into a work queue, waiting for work thread.

package com.wang.chapter4.threadpool;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by 王忠珂 on 2016/11/23.
 */
public class DefaultThreadPool<Job extends Runnable> implements ThreadPool<Job> {
    // 线程最大限制数
    private static int maxWorkerNumber = 10;
    // 线程池默认的数量
    private static int defaultWorkerNumbers = 5;
    // 线程池最小数量
    private static int minWorkerNumbers = 1;
    // 工作列表
    private final LinkedList<Job> jobs  = new LinkedList<Job>();
    // 工作者列表
    private final List<Worker> workers = Collections.synchronizedList(new ArrayList<Worker>());
    // 工作者线程的数量
    private int workerNum = defaultWorkerNumbers;
    // 线程编号
    private AtomicLong threadNum = new AtomicLong();

    public DefaultThreadPool() {
        initializeWorkers(defaultWorkerNumbers);
    }

    public DefaultThreadPool(int num) {
        workerNum = num > maxWorkerNumber ? maxWorkerNumber :
                num < minWorkerNumbers ? minWorkerNumbers : num;
        initializeWorkers(workerNum);
    }

    public DefaultThreadPool(int defaultWorkerNumber, int maxWorkerNumber, int minWorkerNumber) {
        this.maxWorkerNumber = maxWorkerNumber;
        this.minWorkerNumbers = minWorkerNumber;
        workerNum = defaultWorkerNumber > maxWorkerNumber ? maxWorkerNumber :
                defaultWor 大专栏  java线程池技术kerNumber < minWorkerNumbers ? minWorkerNumbers : defaultWorkerNumber;
        initializeWorkers(workerNum);
    }

    @Override
    public void execute(Job job) {
        if (job != null) {
            synchronized (jobs) {
                jobs.addLast(job);
                jobs.notify();
            }
        }
    }

    @Override
    public void shutdown() {
        for (Worker worker: workers) {
            worker.shutdown();
        }
    }

    @Override
    public void addWorkers(int num) {
        synchronized (jobs) {
            if (num + this.workerNum > maxWorkerNumber) {
                num = maxWorkerNumber - this.workerNum;
            }
            initializeWorkers(num);
            this.workerNum += num;
        }
    }

    @Override
    public void removeWorkers(int num) {
        synchronized (jobs) {
            if (num >= this.workerNum) {
                throw new IllegalArgumentException("beyond workNum");
            }
            int count = 0;
            while (count < num) {
                Worker worker = workers.get(count);
                if (workers.remove(worker)) {
                    worker.shutdown();
                    count ++;
                }
            }
            this.workerNum -= count;
        }
    }

    @Override
    public int getJobSize() {
        return jobs.size();
    }

    private void initializeWorkers (int num) {
        for (int i=0; i<num; i++) {
            Worker worker = new Worker();
            workers.add(worker);
            Thread thread = new Thread(worker, "ThreadPool-Worker-" + threadNum.incrementAndGet());
            thread.start();
        }
    }

    class Worker implements Runnable {
        // 是否工作
        private volatile boolean running = true;
        public void run() {
            while (running){
                Job job = null;
                synchronized (jobs) {
                    while (jobs.isEmpty()) {
                        try {
                            jobs.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                    // 取出一个Job
                    job = jobs.removeFirst();
                }
                if (job != null) {
                    try {
                        job.run();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public void shutdown() {
            running = false;
        }
    }
}


Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11718106.html