:
- , (.. ), , .
Fortunately, you do not need to understand the internal details of the disruptor template in order to use it. If it’s easier for you to understand the code, here is the Hello World CoralQueue , the lowest latency interworking queue that implements the destroyer pattern.
package com.coralblocks.coralqueue.sample.queue;
import com.coralblocks.coralqueue.AtomicQueue;
import com.coralblocks.coralqueue.Queue;
import com.coralblocks.coralqueue.util.Builder;
public class Basics {
public static void main(String[] args) {
final Queue<StringBuilder> queue = new AtomicQueue<StringBuilder>(1024, new Builder<StringBuilder>() {
@Override
public StringBuilder newInstance() {
return new StringBuilder(1024);
}
});
Thread producer = new Thread(new Runnable() {
private final StringBuilder getStringBuilder() {
StringBuilder sb;
while((sb = queue.nextToDispatch()) == null) {
}
return sb;
}
@Override
public void run() {
StringBuilder sb;
while(true) {
sb = getStringBuilder();
sb.setLength(0);
sb.append("Hello!");
queue.flush();
sb = getStringBuilder();
sb.setLength(0);
sb.append("Hi!");
sb = getStringBuilder();
sb.setLength(0);
sb.append("Hi again!");
queue.flush();
}
}
}, "Producer");
Thread consumer = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
long avail;
while((avail = queue.availableToPoll()) == 0) {
}
for(int i = 0; i < avail; i++) {
StringBuilder sb = queue.poll();
}
queue.donePolling();
}
}
}, "Consumer");
consumer.start();
producer.start();
}
}
source
share