]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/application/RecorderJobPoolExecutor.java
Merge branch 'master' of ssh://git.xonotic.org/xonotic
[xonotic/xonotic.git] / misc / tools / NexuizDemoRecorder / main / src / main / java / com / nexuiz / demorecorder / application / RecorderJobPoolExecutor.java
1 package com.nexuiz.demorecorder.application;
2
3
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.concurrent.ArrayBlockingQueue;
7 import java.util.concurrent.ThreadPoolExecutor;
8 import java.util.concurrent.TimeUnit;
9
10 import com.nexuiz.demorecorder.application.jobs.RecordJob;
11
12 public class RecorderJobPoolExecutor {
13         
14         private int poolSize = 1;
15         private int maxPoolSize = 1;
16         private long keepAliveTime = 10;
17         private ThreadPoolExecutor threadPool = null;
18         private ArrayBlockingQueue<Runnable> queue = null;
19
20         public RecorderJobPoolExecutor() {
21                 queue = new ArrayBlockingQueue<Runnable>(99999);
22                 threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue);
23         }
24
25         public void runJob(Runnable task) {
26                 threadPool.execute(task);
27         }
28         
29         public void clearUnfinishedJobs() {
30                 threadPool.getQueue().clear();
31         }
32
33         public void shutDown() {
34                 threadPool.shutdownNow();
35         }
36         
37         public synchronized List<RecordJob> getJobList() {
38                 List<RecordJob> list = new ArrayList<RecordJob>();
39                 for (Runnable job : this.queue) {
40                         try {
41                                 RecordJob j = (RecordJob)job;
42                                 list.add(j);
43                         } catch (ClassCastException e) {}
44                 }
45                 
46                 return list;
47         }
48 }