/**
* 基础线程
* @author djk
*
*/
public abstract class BaseThread implements Runnable
{
private Object o;
public Object getO() {
return o;
}
public void setO(Object o) {
this.o = o;
}
@Override
public void run()
{
run(o);
}
/**
* 抽象的run方法
* @param o
*/
public abstract void run(Object o);
}
package com.thread;
import java.util.HashMap;
import java.util.Map;
/**
* 线程工厂
* @author djk
*
*/
public class ThreadFactory
{
/**
* key放入线程的名称。value放入线程的class对象
*/
private static Map<String,Class<?>> map = new HashMap<String, Class<?>>(10);
static
{
map.put("thread1",Thread1.class);
}
/**
* 根据线程名称获得对应的线程
* @param name
* @return
*/
public static BaseThread getThread(String name)
{
BaseThread baseThread = null;
try {
Class<BaseThread> claze = (Class<BaseThread>) map.get(name);
baseThread = claze.newInstance();
} catch (Exception e) {
}
return baseThread;
}
}
package com.thread;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池管理
* @author djk
*
*/
public class ThreadPoolManager
{
/**
* 线程池
*/
private ThreadPoolExecutor executor;
/**
* 任务名称
*/
private String taskName;
public ThreadPoolManager(String taskName, int corePoolSize,
int maximumPoolSize, int queueSize)
{
this.taskName = taskName;
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 1,
TimeUnit.HOURS, new ArrayBlockingQueue<Runnable>(queueSize));
}
/**
* 增加任务
* @param o
*/
public void addTask(Object o)
{
BaseThread thread = ThreadFactory.getThread(taskName);
if (null != thread)
{
thread.setO(o);
executor.execute(thread);
}else
{
System.out.println("thread is not exist....");
}
}
public ThreadPoolExecutor getExecutor() {
return executor;
}
public void setExecutor(ThreadPoolExecutor executor) {
this.executor = executor;
}
}