1 import java.util.concurrent.BlockingQueue;
2 import java.util.concurrent.LinkedBlockingQueue;
3 import java.util.concurrent.ThreadPoolExecutor;
4 import java.util.concurrent.TimeUnit;
5
6 /**
7 * <p>spark 异常处理</p>
8 *
9 * @author
10 * @version V1.0
11 * @modify by user: {修改人} 2015年11月2日
12 * @modify by reason:{方法名}:{原因}
13 */
14 public class SparkThreadScheduler {
15
16 private static SparkThreadScheduler instance;
17
18 private static int taskTotalNum = 2;
19
20 // 阻塞式任务队列,用于存放提交的任务
21 private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(taskTotalNum);
22
23 // 任务处理线程池
24 private final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(taskTotalNum, taskTotalNum, 1,
25 TimeUnit.HOURS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
26
27 /**
28 * 私有构造函数
29 */
30 private SparkThreadScheduler() {
31
32 }
33
34 /**
35 * 获取单例
36 *
37 * @return
38 * @author
39 */
40 public static synchronized SparkThreadScheduler getInstance() {
41 if (null == instance) {
42 instance = new SparkThreadScheduler();
43 }
44 return instance;
45 }
46
47 /**
48 * 执行任务提交
49 *
50 * @param thread
51 * @author
52 */
53 public synchronized void execute(Thread thread) {
54 threadPool.submit(thread);
55 }
56
57 }