java线程(线程池工作示例)

本示例旨于介绍利用线程池去处理相应的频繁工作(以预先分配的一定量固定线程服务于不断添加的工作需求,减少因反复创建、消亡线程造成的系统资源浪费)

工作者线程worker:用于向任务队列申请调取任务,对其进行工作处理、执行相应的操作(线程同步)

任务线程Job:具体任务类,具有自己的处理步骤,交由工作者进行调用

线程池ThreadPool: 生成一定量的工作者线程 及拥有一个Job任务队列存放待处理的任务,一旦队列中有任务,工作者们将争占任务进行处理(线程同步,获取队列锁)


当客户端调用execute(Job) 将会不断地向任务队列中添加任务,而工作者线程会获取通知,从其中取出任务进行处理,当所有任务都完成后,工作者们又进入等待状态(Waiting)  (由于此背景下,所有工作者没有特殊区分,都是进行一样的机械操作,所以当任务进入队列时,只需使用notify() 唤醒一个等待的工作者就可以了,无需将所有工作者都唤醒,我们并不在意唤醒的是几号工作者。)


实例代码如下(无需修改即可运行测试)

public interface ThreadPool {
    // 执行一个Job,这个Job需要实现Runnable
    void execute(Job job);

    // 关闭线程池
    void shutdown();

    // 增加工作者线程
    void addWorkers(int num);

    // 减少工作者线程
    void removeWorker(int num);

    // 得到正在等待执行的任务数量
    int getJobSize();
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
public class DefaultThreadPool implements ThreadPool {
    // 线程池最大限制数
    private static final int      MAX_WORKER_NUMBERS     = 10;
    // 线程池默认的数量
    private static final int      DEFAULT_WORKER_NUMBERS = 5;
    // 线程池最小的数量
    private static final int      MIN_WORKER_NUMBERS     = 1;
    // 这是一个工作列表,将会向里面插入工作
    private final LinkedList jobs                   = new LinkedList();
    // 工作者列表
    private final List    workers                = Collections.synchronizedList(new ArrayList());
    // 工作者线程的数量
    private int                   workerNum              = DEFAULT_WORKER_NUMBERS;
    // 线程编号生成
    private AtomicLong            threadNum              = new AtomicLong();

    public DefaultThreadPool() {
        initializeWokers(DEFAULT_WORKER_NUMBERS);
    }
    public DefaultThreadPool(int num) {
        workerNum = num > MAX_WORKER_NUMBERS ? MAX_WORKER_NUMBERS : num < MIN_WORKER_NUMBERS ? MIN_WORKER_NUMBERS : num;
        initializeWokers(workerNum);
    }
    public void execute(Job job) {
        if (job != null) {
            // 添加一个工作,然后进行通知
            synchronized (jobs) {
                jobs.addLast(job);
                jobs.notify();
            }
        }
    }
    public void shutdown() {
        for (Worker worker : workers) {
            worker.shutdown();
        }
    }
    public void addWorkers(int num) {
        synchronized (jobs) {
            // 限制新增的Worker数量不能超过最大值
            if (num + this.workerNum > MAX_WORKER_NUMBERS) {
                num = MAX_WORKER_NUMBERS - this.workerNum;
            }
            initializeWokers(num);
            this.workerNum += num;
        }
    }
    public void removeWorker(int num) {
        synchronized (jobs) {
            if (num >= this.workerNum) {
                throw new IllegalArgumentException("beyond workNum");
            }
            // 按照给定的数量停止Worker
            int count = 0;
            while (count < num) {
                workers.get(count).shutdown();
                count++;
            }
            this.workerNum -= count;
        }
    }
    public int getJobSize() {
        return jobs.size();
    }
    // 初始化线程工作者
    private void initializeWokers(int num) {
        for (int i = 0; i < num; i++) {
            Worker worker = new Worker(i+1);
            workers.add(worker);
            Thread thread = new Thread(worker, "ThreadPool-Worker-" + threadNum.incrementAndGet());
            thread.start();
        }
    }
    // 工作者,负责消费任务
    class Worker implements Runnable {
        // 是否工作
    	private int workerID;
    	public Worker(int ID){
    		this.workerID=ID;
    	}
        private volatile boolean running = true;
        public void run() {
            while (running) {
                Job job = null;
                synchronized (jobs) {
                    // 如果工作者列表是空的,那么就wait
                    while (jobs.isEmpty()) {
                        try {
                        	System.out.println("jobs is empty and worker "+workerID+" will sleep");
                            jobs.wait();
                        	System.out.println("there is a job and "+workerID+" go on");
                        } catch (InterruptedException ex) {
                            // 感知到外部对WorkerThread的中断操作,返回
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                    // 取出一个Job
                    job = jobs.removeFirst();
                }
                if (job != null) {
                    try {
                        job.run();
                    } catch (Exception ex) {
                        // 忽略Job执行中的Exception
                    }
                }
            }
        }
        public void shutdown() {
            running = false;
        }
    }
}

public class DefaultThreadPoolTest {

	public static void main(String[] args) {
		DefaultThreadPool dThreadPool=new DefaultThreadPool();
		for(int n=1;n<=5;n++){
			dThreadPool.execute(new Job("Job Thread of "+n));
		}
	}
	static class Job implements Runnable{
	private String stt;
		public Job(String stt){
			this.stt=stt;
		}
		@Override
		public void run() {
			for(int i=1;i<=3;i++)
			System.out.println(stt+" in"+i);
		}
	}
}

这例子的好处之一是体现了解耦性,工作者worker不关心Job类如何实现,只需调用其run(),而具体操作在Job本身中体现。在Test测试中,我让每个Job类输出3次循环,而main中 添加了5个Job进线程池ThreadPool,当任务列表中没有任务时,worker会输出表示当前无任务,此线程进入等待,等到重新被唤醒再继续执行。

运行结果:

jobs is empty and worker 1 will sleep
jobs is empty and worker 5 will sleep
jobs is empty and worker 4 will sleep
jobs is empty and worker 3 will sleep
jobs is empty and worker 2 will sleep
there is a job and 1 go on
there is a job and 5 go on
Job Thread of 1 in1
there is a job and 4 go on
Job Thread of 2 in1
Job Thread of 2 in2
Job Thread of 3 in1
Job Thread of 1 in2
Job Thread of 3 in2
Job Thread of 2 in3
there is a job and 3 go on
Job Thread of 3 in3
Job Thread of 1 in3
Job Thread of 5 in1
there is a job and 2 go on
Job Thread of 4 in1
jobs is empty and worker 2 will sleep
Job Thread of 5 in2
jobs is empty and worker 1 will sleep
Job Thread of 4 in2
jobs is empty and worker 4 will sleep
Job Thread of 5 in3
Job Thread of 4 in3
jobs is empty and worker 5 will sleep
jobs is empty and worker 3 will sleep

希望借助这个例子能让大家对线程池及线程的等待、唤醒等机制有更深入的理解,而不仅仅只是知道有这些方法。注意,worker线程在获得列表集合锁后进行判断当前集合内是否有待处理的任务工作,若有则取出任务(将此任务从列表中删除),若没有任务,则worker进行wait()等待并释放列表锁,一旦有任务添加,且此线程被唤醒时,再继续执行wait() 的下一行代码,具体细节请通过本例子中的 jobs is empty and worker 1 will sleep 和 there is a job and 1 go on 两句输出进行理解。


你可能感兴趣的:(java线程)