Mercurial > hg > Database > Alice
changeset 490:f28087efa911 dispose
set priority CodeSegment
author | sugi |
---|---|
date | Wed, 10 Dec 2014 18:47:21 +0900 |
parents | 9a7dd7591ddc |
children | 7d67bc538549 |
files | src/main/java/alice/codesegment/CodeSegment.java src/main/java/alice/codesegment/CodeSegmentManager.java |
diffstat | 2 files changed, 43 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/alice/codesegment/CodeSegment.java Tue Dec 09 14:25:57 2014 +0900 +++ b/src/main/java/alice/codesegment/CodeSegment.java Wed Dec 10 18:47:21 2014 +0900 @@ -10,6 +10,7 @@ public InputDataSegment ids = new InputDataSegment(this); public OutputDataSegment ods = new OutputDataSegment(); private ArrayList<Receiver> list = new ArrayList<Receiver>(); + private int priority = Thread.NORM_PRIORITY; public void execute() { ids.receive(); @@ -28,4 +29,11 @@ } } + public int getPriority() { + return priority; + } + + public void setPriority(int priority) { + this.priority = priority; + } }
--- a/src/main/java/alice/codesegment/CodeSegmentManager.java Tue Dec 09 14:25:57 2014 +0900 +++ b/src/main/java/alice/codesegment/CodeSegmentManager.java Wed Dec 10 18:47:21 2014 +0900 @@ -1,6 +1,7 @@ package alice.codesegment; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -9,23 +10,52 @@ public class CodeSegmentManager { private static CodeSegmentManager instance = new CodeSegmentManager(); - private ThreadPoolExecutor codeSegmentExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads - Runtime.getRuntime().availableProcessors(), - Integer.MAX_VALUE, // keepAliveTime - TimeUnit.SECONDS, - new LinkedBlockingQueue<Runnable>()); + private ThreadPoolExecutor codeSegmentExecutor; + + private ThreadFactory maxPriority; + private ThreadFactory minPriority; + private ThreadFactory normalPriority; + private Logger logger = Logger.getLogger(CodeSegmentManager.class); + private CodeSegmentManager() { + codeSegmentExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads + Runtime.getRuntime().availableProcessors(), + Integer.MAX_VALUE, // keepAliveTime + TimeUnit.SECONDS, + new LinkedBlockingQueue<Runnable>()); + normalPriority = codeSegmentExecutor.getThreadFactory(); + maxPriority = makeThreadFactory(Thread.MAX_PRIORITY); + minPriority = makeThreadFactory(Thread.MIN_PRIORITY); + } + public static CodeSegmentManager get() { return instance; } public static void submit(CodeSegment cs) { CodeSegmentManager csManager = CodeSegmentManager.get(); + if (cs.getPriority() < Thread.NORM_PRIORITY) { + csManager.codeSegmentExecutor.setThreadFactory(csManager.minPriority); + } else if (cs.getPriority() < Thread.MAX_PRIORITY) { + csManager.codeSegmentExecutor.setThreadFactory(csManager.normalPriority); + } else { + csManager.codeSegmentExecutor.setThreadFactory(csManager.maxPriority); + } csManager.codeSegmentExecutor.execute(cs); if (csManager.logger.isDebugEnabled()) { csManager.logger.debug(cs.getClass().getName()); } } + private ThreadFactory makeThreadFactory(final int priority) { + return new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setPriority(priority); + return thread; + } + }; + } }