Skip to content

Commit 45cf108

Browse files
author
chengyitian
committed
AJ-763: optimize code logic for ThreadPooledClient;
1 parent 2b8772b commit 45cf108

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/com/xxdb/streaming/client/ThreadPooledClient.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.concurrent.ExecutorService;
1717
import java.util.concurrent.Executors;
1818
import java.util.concurrent.TimeUnit;
19+
import java.util.concurrent.locks.LockSupport;
1920

2021

2122
public class ThreadPooledClient extends AbstractClient {
@@ -58,14 +59,14 @@ public ThreadPooledClient(String subscribeHost, int subscribePort, int threadCou
5859

5960
this.threadCount = threadCount;
6061
threadPool = Executors.newFixedThreadPool(threadCount);
62+
6163
new Thread() {
6264
private LinkedList<IMessage> backlog = new LinkedList<>();
6365

6466
private boolean fillBacklog() {
6567
boolean filled = false;
6668
synchronized (queueHandlers) {
67-
Set<String> keySet = queueHandlers.keySet();
68-
for (String topic : keySet) {
69+
for (String topic : queueHandlers.keySet()) {
6970
List<IMessage> messages = queueHandlers.get(topic).queue.poll();
7071
if (messages != null) {
7172
backlog.addAll(messages);
@@ -79,20 +80,17 @@ private boolean fillBacklog() {
7980
private void refill() {
8081
int count = 200;
8182
while (!fillBacklog()) {
82-
if (count > 100) {
83-
;
84-
} else if (count > 0) {
83+
if (count > 0)
8584
Thread.yield();
86-
} else {
87-
Thread.yield();
88-
//LockSupport.park();
89-
}
85+
else
86+
// Pause for 1 millisecond
87+
LockSupport.parkNanos(1000 * 1000);
9088
count = count - 1;
9189
}
9290
}
9391

9492
public void run() {
95-
while (true) {
93+
while (!Thread.currentThread().isInterrupted()) {
9694
IMessage msg;
9795
while ((msg = backlog.poll()) != null) {
9896
QueueHandlerBinder binder;

0 commit comments

Comments
 (0)