Showing
1 changed file
with
29 additions
and
19 deletions
@@ -23,53 +23,63 @@ public class RunnableQueueFactory { | @@ -23,53 +23,63 @@ public class RunnableQueueFactory { | ||
23 | private ExecutorService executorService = Executors.newFixedThreadPool(10); | 23 | private ExecutorService executorService = Executors.newFixedThreadPool(10); |
24 | 24 | ||
25 | /** | 25 | /** |
26 | - * add task | ||
27 | - * @param taskType | ||
28 | - * @param task | 26 | + * add a task |
27 | + * @param type the queue type | ||
28 | + * @param task the task | ||
29 | */ | 29 | */ |
30 | - public void addTask(String taskType, Runnable task){ | ||
31 | - BlockingQueue<Runnable> queue = queueMap.get(taskType); | 30 | + public void addTask(String type, Runnable task){ |
31 | + BlockingQueue<Runnable> queue = queueMap.get(type); | ||
32 | if(queue==null){ | 32 | if(queue==null){ |
33 | queue = new ArrayBlockingQueue<>(MAX_QUEUE_SIZE); | 33 | queue = new ArrayBlockingQueue<>(MAX_QUEUE_SIZE); |
34 | - putTask(queue,taskType,task); | ||
35 | - queueMap.putIfAbsent(taskType,queue); | ||
36 | - initQueue(taskType); | 34 | + putTask(queue,type,task); |
35 | + queueMap.putIfAbsent(type,queue); | ||
36 | + initQueue(type); | ||
37 | }else{ | 37 | }else{ |
38 | - putTask(queue,taskType,task); | 38 | + putTask(queue,type,task); |
39 | } | 39 | } |
40 | 40 | ||
41 | } | 41 | } |
42 | 42 | ||
43 | - private void putTask(BlockingQueue<Runnable> queue,String taskType,Runnable task){ | 43 | + /** |
44 | + * put a task to the queue | ||
45 | + * @param queue the queue | ||
46 | + * @param type the queue type | ||
47 | + * @param task the task | ||
48 | + */ | ||
49 | + private void putTask(BlockingQueue<Runnable> queue,String type,Runnable task){ | ||
44 | try { | 50 | try { |
45 | queue.put(task); | 51 | queue.put(task); |
46 | - LOGGER.info("addTask success,with taskType=[{}]",taskType); | 52 | + LOGGER.info("put a task with type=[{}] to queue successfully",type); |
47 | } catch (InterruptedException e) { | 53 | } catch (InterruptedException e) { |
48 | - LOGGER.error("put task error,with taskType=[{}],cause:",taskType,e); | 54 | + LOGGER.error("put a task with type=[{}] to queue error,cause:",type,e); |
49 | } | 55 | } |
50 | } | 56 | } |
51 | 57 | ||
52 | - private void initQueue(String taskType){ | 58 | + /** |
59 | + * init the queue | ||
60 | + * @param type the queue type | ||
61 | + */ | ||
62 | + private void initQueue(String type){ | ||
53 | executorService.execute(new Runnable() { | 63 | executorService.execute(new Runnable() { |
54 | @Override | 64 | @Override |
55 | public void run() { | 65 | public void run() { |
56 | - BlockingQueue<Runnable> queue = queueMap.get(taskType); | 66 | + BlockingQueue<Runnable> queue = queueMap.get(type); |
57 | if(queue!=null){ | 67 | if(queue!=null){ |
58 | - LOGGER.info("initQueue success,with taskType=[{}]",taskType); | 68 | + LOGGER.info("initQueue with type=[{}] successfully",type); |
59 | try { | 69 | try { |
60 | for(;;) { | 70 | for(;;) { |
61 | - LOGGER.info("try to take a task from queue,with taskType=[{}]",taskType); | 71 | + LOGGER.info("try to take a task with type=[{}] from queue",type); |
62 | Runnable task = queue.take(); | 72 | Runnable task = queue.take(); |
63 | if(task!=null) { | 73 | if(task!=null) { |
64 | - LOGGER.info("take a task success,with taskType=[{}],will run it directly",taskType); | 74 | + LOGGER.info("take a task with type=[{}] from queue successfully,will run it directly",type); |
65 | task.run(); | 75 | task.run(); |
66 | } | 76 | } |
67 | } | 77 | } |
68 | }catch(Exception e){ | 78 | }catch(Exception e){ |
69 | - LOGGER.error("take task error,with taskType=[{}],cause:",taskType,e); | 79 | + LOGGER.error("take task with type=[{}] error,cause:",type,e); |
70 | } | 80 | } |
71 | }else{ | 81 | }else{ |
72 | - LOGGER.error("initQueue failed,with queue=null and taskType=[{}]",taskType); | 82 | + LOGGER.error("initQueue with type=[{}] failed,because the queue is null ",type); |
73 | } | 83 | } |
74 | } | 84 | } |
75 | }); | 85 | }); |
-
Please register or login to post a comment