Skip to content

Wait Strategies for working with Wait Free Queues

johnmcclean-aol edited this page Aug 29, 2015 · 2 revisions

Wait Strategies can be plugged into async.Queues for working with non-blocking / wait free queues. Separate (or identical implementations) can be plugged in to handle production and consumption in the same Queue.

Available wait strategies are

  • DirectWaitStrategy : offer / poll and return immediately - no retry Will try to access the queue once, and return the result directly from the Queue, effectively the same as calling queue.take() / queue.offer(T val)

E.g. Queue using DirectWaitStrategy for both consumption (2nd parameter) and production (3rd parameter) -

Queue<String> q = new Queue<>(new ManyToOneConcurrentArrayQueue<String>(100),
									new DirectWaitStrategy<>(),
									new DirectWaitStrategy<>());

		
q.offer("hello");
q.stream().forEach(System.out::println);
  • ExponentialBackofWaitStrategy : retry with an exponentially increasing backoff period
Queue<String> q = new Queue<>(new ManyToOneConcurrentArrayQueue<String>(100),
									new ExponentialBackofWaitStrategy<>(),
									new ExponentialBackofWaitStrategy<>());
		
		q.offer("hello");
		q.stream().forEach(System.out::println);
  • NoWaitRetry : constantly offer /poll until data or space is available
Queue<String> q = new Queue<>(new ManyToOneConcurrentArrayQueue<String>(100),
									new NoWaitRetry<>(),
									new NoWaitRetry<>());
		
q.offer("hello");
q.stream().forEach(System.out::println);
  • SpinWait : constantly retry but park for 1 nanosecond between each retry
Queue<String> q = new Queue<>(new ManyToOneConcurrentArrayQueue<String>(100),
									new SpinWait<>(),
									new SpinWait<>());
		
q.offer("hello");
q.stream().forEach(System.out::println);
  • YieldWait : constantly retry but yield between each retry
Queue<String> q = new Queue<>(new ManyToOneConcurrentArrayQueue<String>(100),
									new YieldWait<>(),
									new YieldWait<>());
		
q.offer("hello");
q.stream().forEach(System.out::println);
Clone this wiki locally