Mercurial Hosting > nabble
comparison src/fschmidt/util/executor/ThreadPool.java @ 68:00520880ad02
add fschmidt source
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Oct 2025 17:24:15 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:9d0fefce6985 | 68:00520880ad02 |
---|---|
1 package fschmidt.util.executor; | |
2 | |
3 import java.util.Queue; | |
4 import java.util.LinkedList; | |
5 import java.util.concurrent.Executor; | |
6 import java.util.concurrent.TimeUnit; | |
7 import java.util.concurrent.atomic.AtomicInteger; | |
8 import org.slf4j.Logger; | |
9 import org.slf4j.LoggerFactory; | |
10 | |
11 | |
12 public final class ThreadPool extends AbstractThreadPool implements Executor { | |
13 private static final Logger logger = LoggerFactory.getLogger(ThreadPool.class); | |
14 | |
15 private final Queue<Runnable> queue = new LinkedList<Runnable>(); | |
16 | |
17 public ThreadPool(int size) { | |
18 super(size); | |
19 start(); | |
20 } | |
21 | |
22 @Override public final void execute(Runnable command) { | |
23 dispatch(command); | |
24 } | |
25 | |
26 public final synchronized boolean dispatch(Runnable command) { | |
27 if( !isRunning() ) | |
28 return false; | |
29 queue.add(wrap(command)); | |
30 notify(); | |
31 return true; | |
32 } | |
33 | |
34 @Override synchronized Runnable getCommand() { | |
35 Runnable command; | |
36 while( (command = queue.poll()) == null ) { | |
37 if( !isRunning() ) | |
38 return null; | |
39 try { | |
40 wait(); | |
41 } catch(InterruptedException e) { | |
42 logger.error("",e); | |
43 } | |
44 } | |
45 return command; | |
46 } | |
47 | |
48 @Override public final synchronized int getQueueSize() { | |
49 return queue.size(); | |
50 } | |
51 | |
52 } |