comparison src/alice/codesegment/CodeSegmentManager.java @ 21:145667a554ad

run CodeSegment with ThreadPoolExecutor
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Sun, 15 Jan 2012 19:02:01 +0900
parents 0bb03861b5cd
children 3155337e754e
comparison
equal deleted inserted replaced
20:0bb03861b5cd 21:145667a554ad
1 package alice.codesegment; 1 package alice.codesegment;
2 2
3 import java.util.concurrent.LinkedBlockingQueue; 3 import java.util.concurrent.LinkedBlockingQueue;
4 import java.util.concurrent.ThreadPoolExecutor;
5 import java.util.concurrent.TimeUnit;
4 6
5 public class CodeSegmentManager { 7 public class CodeSegmentManager {
6 private static CodeSegmentManager instance = new CodeSegmentManager(); 8 private static CodeSegmentManager instance = new CodeSegmentManager();
7 public LinkedBlockingQueue<CodeSegment> readyQueue = new LinkedBlockingQueue<CodeSegment>(); 9 public LinkedBlockingQueue<CodeSegment> readyQueue = new LinkedBlockingQueue<CodeSegment>();
10 ThreadPoolExecutor codeSegmentExecutor = new ThreadPoolExecutor(1,
11 Runtime.getRuntime().availableProcessors(),
12 Integer.MAX_VALUE,
13 TimeUnit.SECONDS,
14 new LinkedBlockingQueue<Runnable>());
8 15
9 private CodeSegmentManager() { 16 private CodeSegmentManager() {
10 Runnable prepareThread = new Runnable() { 17 Runnable prepareThread = new Runnable() {
11 18
12 @Override 19 @Override
13 public void run() { 20 public void run() {
14 while (true) { 21 while (true) {
15 try { 22 try {
16 CodeSegment cs = readyQueue.take(); 23 CodeSegment cs = readyQueue.take();
17 cs.run(); 24 codeSegmentExecutor.execute(cs);
18 } catch (InterruptedException e) { 25 } catch (InterruptedException e) {
19 e.printStackTrace(); 26 e.printStackTrace();
20 } 27 }
21 } 28 }
22 } 29 }