Sequential Concurrency ... WHAT ???


Published on

Using the ThreadPool, we cannot ensure the execution order of the tasks. There are few situations as per the industry needs, where we need to ensure the task execution order. StripedExecutorService class we are developing, would ensure the task execution order. Example: On an Exchange server we have many products on which users can trade. Transactions on a single product should be performed in FIFO order as per the business needs, and there are many products of such type. So along with maintaining the sequential order of execution of tasks for A product, we are also processing tasks sequentially for other product B as well concurrently. We would be showing Heinz Kabutz implementation.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Sequential Concurrency ... WHAT ???

  1. 1. Jitendra Chittoda, Member Development Department @ ION Trading IndiaPvt. Ltd.Co-leader Delhi and NCR Java User Group (DaNJUG)Email: jitendra@chittoda.comTwitter: @JChittodaBlog: http://chittoda.comSequential Concurrency…. WHAT???08 May 20131JavaOne India 2013 : COM1240
  2. 2. Delhi & NCR JUG (DaNJUG)08 May 20132 30+ Members Looking for more active members, Groups If your friends are in Delhi NCR, please spreadthe word
  3. 3. Agenda What is ThreadPool Benefits of using ThreadPool What we can’t achieve with ThreadPool Why & Where ? Sequential Concurrency Fundamentals SerialExecutor StripedExecutorService Design Features Current State08 May 20133
  4. 4. ThreadPool08 May 20134
  5. 5. Benefits of ThreadPool08 May 20135 Pre-initialized pool of threads Reusable threads Configurable
  6. 6. What we can’t achieve withThreadPool08 May 20136 As per the business requirement, sometimes youmay want to process Tasks in specific order. Tasks execution order is not maintained by thenormal ThreadPools Processing Not Ordered Processing of a Task is not ordered, you can say itsuncertain.
  7. 7. 08 May 20137
  8. 8. Why? Industry Examples08 May 20138 Exchange trading Processing of trades on an instrument. Train Reservations Ticket booking requests for a train must beprocessed in FIFO order. Multi Tenancy Each tenant want some specific execution to beordered
  9. 9. Where ?08 May 20139 First saw in 2007 then in 2010 and in 2011 November 2011: Started writing my ownimplementation, published a blog on November 2012: Heinz got 5 different implementations after thisissue.
  10. 10. Sequential ConcurrencyFundamentals08 May 201310•Identification•Maintaining Order•Sequential Processing•No Dependency
  11. 11. Sequential ConcurrencyFundamentals08 May 201311 Identification• Maintain the sequence based on some Stripe orKey• Stripe: An object which is common among theTasks those needs to be processed in sequence.
  12. 12. Sequential ConcurrencyFundamentals08 May 201312 Maintaining Order• Maintain the sequential ordering tasks• FIFO Order
  13. 13. Sequential ConcurrencyFundamentals08 May 201313 Sequential Processing Process tasks of a Stripe in sequence To maintain this we assign one and only one Thread to aQueue.
  14. 14. Sequential ConcurrencyFundamentals08 May 201314 No Dependency Tasks of one Stripe-X should not be dependent on Tasks ofanother Stripe-Y
  15. 15. Sequential Concurrency08 May 20131512 132Pool ofThreadsQueue ofStripeStripedExecutorServiceT-1T-2T-3
  16. 16. Executor JavaDoc08 May 201316• Many Executor implementations impose some sort oflimitation on how and when tasks are scheduled. Theexecutor below serializes the submission of tasks to asecond executor, illustrating a composite executor.class SerialExecutor implements Executor {Queue<Runnable> tasks = new ArrayDeque<>();Executor executor;Runnable active;SerialExecutor(Executor executor) {this.executor = executor;}
  17. 17. SerialExecutor08 May 201317public void execute(final Runnable r) {tasks.offer(new Runnable() {public void run() {try {;} finally {scheduleNext();}}});if (active == null) {scheduleNext();}}
  18. 18. SerialExecutor08 May 201318protected void scheduleNext() {if ((active = tasks.poll()) != null) {executor.execute(active);}}
  19. 19. Pool of SerialExecutor08 May 201319 SerialExecutor maintain the sequence of thetasks assigned to it and process them insequence. Using single queue. BUT we need pool of SerialExecutor, so that wecan concurrently execute tasks of two or moreStriped Queues
  20. 20. Implementation Details08 May 2013201) Identification• Stripe: We are using Object as Stripe, you candefine your own.2) Maintain Order• With separate Queue based on Stripe.• SerialExecutor is maintaining a Queue3) Sequential Processing• Handled by SerialExecutor4) No Dependency• Handled by SerialExecutor
  21. 21. Design08 May 201321
  22. 22. 08 May 201322
  23. 23. 08 May 201323
  24. 24. Inside StripedExecutorService08 May 201324class SerialExecutor implements Executor {private BlockingQueue<Runnable> tasks =new LinkedBlockingQueue<>();private Runnable active;private final Object stripe;private SerialExecutor(Object stripe) {this.stripe = stripe;}
  25. 25. 08 May 201325public void execute(final Runnable r) {lock.lock();try {tasks.add(new Runnable() {public void run() {try {;} finally { scheduleNext(); }}});if (active == null){ scheduleNext(); }} finally {lock.unlock();}}
  26. 26. Inside StripedExecutorService08 May 201326private void scheduleNext() {lock.lock();try {if ((active = tasks.poll()) != null) {executor.execute(active);terminating.signalAll();} else {removeEmptySerialExecutor(stripe, this);}} finally {lock.unlock();}}
  27. 27. 08 May 201327private final ExecutorService executor;private final Map<Object, SerialExecutor> executors =new IdentityHashMap<>();public void execute(Runnable command) {lock.lock();try {Object stripe = getStripe(command);if (stripe != null) {SerialExecutor ser_exec=executors.get(stripe);if (ser_exec == null) {executors.put(stripe, ser_exec=new SerialExecutor(stripe));}ser_exec.execute(command);} else { executor.execute(command); }} finally { lock.unlock(); }}
  28. 28. Features08 May 201328 Non striped tasks can also be handled with SES Queues gets created when required No memory leaks in terms of the Queues. Queuesgets removed once all tasks are finished.
  29. 29. Current State08 May 201329 Work In Progress Several other implementations available, but noone passes the tests written forStripedExecutorService Performance
  30. 30. References08 May 201330 Delhi & NCR JUG GitHub : Dr. Heinz Kabutz Newsletter Blog
  31. 31. 08 May 201331