Skip to content

Instantly share code, notes, and snippets.

@arnaudroger
Created January 30, 2019 17:21
Show Gist options
  • Save arnaudroger/ef2ef24c46cb10cbca1894de0b8e6b89 to your computer and use it in GitHub Desktop.
Save arnaudroger/ef2ef24c46cb10cbca1894de0b8e6b89 to your computer and use it in GitHub Desktop.
import org.jctools.queues.MpscUnboundedArrayQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
public class Main {
public static final long HEALTH_INT = TimeUnit.SECONDS.toNanos(1);
public static volatile long l;
public static void main(String[] args) {
MpscUnboundedArrayQueue<Long> queue = new MpscUnboundedArrayQueue<Long>(1024);
Thread thealth = new Thread(() -> {
do {
LockSupport.parkNanos(HEALTH_INT);
System.out.println(" healthy " + l);
} while (true);
}, "thealth");
Thread tOffer = new Thread(() -> {
long l = 0;
do {
queue.offer(l++);
LockSupport.parkNanos(1);
} while(true);
}, "toffer");
Thread tConsumer = new Thread(() -> {
do {
Long ll = queue.poll();
if (ll != null)
Main.l = ll;
LockSupport.parkNanos(1);
} while(true);
}, "tpoll");
thealth.start();
tConsumer.start();
tOffer.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment