线程池管理服务

线程池管理服务类


package com.bill99.fpd.mfs.common;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池管理服务
 */
public class ThreadPoolUtils {
	
	private int threadPoolSize = 10;
	
	/**
	 * 当前任务执行线程池
	 */
	private ExecutorService executor = null;
	
	/**
	 * 初始化操作
	 */
	public void init() {
		executor = Executors.newFixedThreadPool(threadPoolSize);
	}
	
	/**
	 * 销毁操作
	 */
	public void destory() {
		if (null != executor) {
			executor.shutdown();
		}
	}
	
	/**
	 * 获取当前线程池线程总数
	 */
	public int getThreadPoolSize() {
		return this.threadPoolSize;
	}
	
	/**
	 * 获取当前活动线程数
	 */
	public int getActiveThreadSize() {
		return ((ThreadPoolExecutor)this.getExecutor()).getActiveCount();
	}

	/**
	 * 异步执行指定任务
	 * @param task
	 */
	public void execute(Runnable task) {
		getExecutor().execute(task);
	}

	public void setThreadPoolSize(int threadPoolSize) {
		this.threadPoolSize = threadPoolSize;
	}

	private ExecutorService getExecutor() {
		if(null == this.executor) {
			init();
		}
		return this.executor;
	}
}


开启线程池

		  threadPoolUtils.execute(new Runnable() {
			public void run() {
				。。。。。
			}//run
		});



你可能感兴趣的:(java)