Skip to content
Newer
Older
100644 78 lines (56 sloc) 1.73 KB
05eef25 Added README
Daniel authored
1 SimpleThreadPoolExecutor
2 ============
3
0e650a4 SimpleExecutorService: executeImmediately not throwing InterruptedExc…
Daniel authored
4 A simple thread pool executor using the strategy trying to execute commands first, else enqueue them, else blocking the calling thread.
05eef25 Added README
Daniel authored
5
6 This executor is trying to execute commands immediately using up to maxPoolSize threads.
7 If the pool cannot be further extended then the commands are queued up to maxQueuedTasks.
8 If even the queue is full then the execute methods will block the calling thread.
9
10 The SimpleThreadPoolExecutor is compatible with ExecutorService and hence compatible with Executor as well.
11
12 This class is intentionally compatible to Spring configuration.
13
2fdb3bc Added API link to README
Daniel authored
14 API documentation: http://jceme.github.io/SimpleThreadPoolExecutor/apidocs/
15
05eef25 Added README
Daniel authored
16 ```java
17 public class Example {
18
19 // ...
20
21 private SimpleExecutorService executor;
22
23
24 /**
25 * Creates a new executor with some reasonable example settings.
26 */
27 @PostConstruct
28 public void init() {
29 SimpleThreadPoolExecutor executor = new SimpleThreadPoolExecutor();
30
31 executor.setMaxPoolSize(20);
32 executor.setCorePoolSize(5);
33 executor.setThreadTimeout(30L, TimeUnit.SECONDS);
34
35 this.executor = executor;
36 }
37
5af0569 Added missing shutdown method to README doc
Daniel authored
38 /**
39 * Shutdown executor when done.
40 */
41 @PreDestroy
42 public void shutdown() {
43 executor.shutdownNow();
44 }
45
05eef25 Added README
Daniel authored
46
47 public void runExample() {
48 Executor executor = this.executor;
49
50 executor.execute(new Runnable() {
51 @Override
52 public void run() {
53 // Do some work here ...
54 }
55 });
56 }
57
58 public void runExampleWithWaitingTimeout() {
59 boolean done = executor.execute(2000L, TimeUnit.MILLISECONDS, new Runnable() {
60 @Override
61 public void run() {
62 // Do some work here ...
63 }
64 });
65
66 if (!done) {
67 log.info("Could not schedule job within two seconds");
68 return;
69 }
70 // else job was scheduled for execution
71 }
72
73
74 // ...
75
76 }
77 ```
Something went wrong with that request. Please try again.