1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.netty.channel.socket.nio;
18
19 import java.util.concurrent.Executor;
20 import java.util.concurrent.atomic.AtomicInteger;
21
22 import org.jboss.netty.channel.Channel;
23 import org.jboss.netty.channel.socket.Worker;
24 import org.jboss.netty.util.ExternalResourceReleasable;
25 import org.jboss.netty.util.internal.ExecutorUtil;
26
27
28
29
30
31 public abstract class AbstractNioWorkerPool<E extends AbstractNioWorker>
32 implements WorkerPool<E>, ExternalResourceReleasable {
33
34 private final AbstractNioWorker[] workers;
35 private final AtomicInteger workerIndex = new AtomicInteger();
36 private final Executor workerExecutor;
37 private final boolean allowShutDownOnIdle;
38
39
40
41
42
43
44
45
46
47
48 AbstractNioWorkerPool(Executor workerExecutor, int workerCount, boolean allowShutDownOnIdle) {
49 if (workerExecutor == null) {
50 throw new NullPointerException("workerExecutor");
51 }
52 if (workerCount <= 0) {
53 throw new IllegalArgumentException(
54 "workerCount (" + workerCount + ") " +
55 "must be a positive integer.");
56 }
57 workers = new AbstractNioWorker[workerCount];
58
59 for (int i = 0; i < workers.length; i++) {
60 workers[i] = createWorker(workerExecutor);
61 }
62 this.allowShutDownOnIdle = allowShutDownOnIdle;
63 this.workerExecutor = workerExecutor;
64
65 }
66
67
68
69
70
71
72
73 AbstractNioWorkerPool(Executor workerExecutor, int workerCount) {
74 this(workerExecutor, workerCount, false);
75 }
76
77
78
79
80
81
82
83
84
85
86
87 @Deprecated
88 protected abstract E createWorker(Executor executor, boolean allowShutdownOnIdle);
89
90
91
92
93
94
95
96
97 protected E createWorker(Executor executor) {
98 return createWorker(executor, allowShutDownOnIdle);
99 }
100
101 @SuppressWarnings("unchecked")
102 public E nextWorker() {
103 return (E) workers[Math.abs(workerIndex.getAndIncrement() % workers.length)];
104 }
105
106
107 public void releaseExternalResources() {
108 ExecutorUtil.terminate(workerExecutor);
109 }
110
111 }