<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div dir="ltr" id="yui_3_16_0_1_1418752108814_10538">Thanks for your response. Will notifyAll() instead of notify() solve the problem?<br></div><div dir="ltr" id="yui_3_16_0_1_1418752108814_10538"><span><br></span></div><div dir="ltr" id="yui_3_16_0_1_1418752108814_10538"><span id="yui_3_16_0_1_1418752108814_10545">Regards</span></div><div dir="ltr" id="yui_3_16_0_1_1418752108814_10538"><span>Suman</span></div><br>  <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" id="yui_3_16_0_1_1418752108814_10464"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" id="yui_3_16_0_1_1418752108814_10463"> <div dir="ltr" id="yui_3_16_0_1_1418752108814_10462"> <hr size="1" id="yui_3_16_0_1_1418752108814_10465">  <font size="2" face="Arial" id="yui_3_16_0_1_1418752108814_10461"> <b><span style="font-weight:bold;">From:</span></b> Oleksandr Otenko <oleksandr.otenko@oracle.com><br> <b><span style="font-weight: bold;">To:</span></b> suman shil <suman_krec@yahoo.com>; Concurrency-interest <concurrency-interest@cs.oswego.edu> <br> <b><span style="font-weight: bold;">Sent:</span></b> Wednesday, December 17, 2014 9:55 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [concurrency-interest] Enforcing ordered execution of critical sections?<br> </font> </div> <div class="y_msg_container" id="yui_3_16_0_1_1418752108814_10536"><br><div id="yiv3812978217"><div id="yui_3_16_0_1_1418752108814_10535">
    There is no guarantee you'll ever hand over the control to the right
    thread upon notify()<br clear="none">
    <br clear="none">
    Alex<br clear="none">
    <br clear="none">
    <div class="qtdSeparateBR"><br><br></div><div class="yiv3812978217yqt6832092596" id="yiv3812978217yqt39457"><div class="yiv3812978217moz-cite-prefix" id="yui_3_16_0_1_1418752108814_10537">On 17/12/2014 14:07, suman shil wrote:<br clear="none">
    </div>
    <blockquote type="cite" id="yui_3_16_0_1_1418752108814_10534">
      <div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;" id="yui_3_16_0_1_1418752108814_10533">
        <div dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786"><span>Hi,</span></div>
        <div dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786">Following is
          my solution to solve this problem. Please let me know if I am
          missing something.</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><br clear="none">
        </div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style="">public class OrderedExecutor</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style="">{</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>private
          int currentAllowedOrder = 0;</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>private
          int maxLength = 0;</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>public
          OrderedExecutor(int n)</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>{</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>   
              this.maxLength = n;</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>}</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span></div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span></div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>public
          synchronized Object execCriticalSectionInOrder(</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>  
                                        int order,</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>  
                                        Callable<Object> callable)</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>  
                                      throws Exception</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>{</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>if
          (order >= maxLength)</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" id="yiv3812978217yui_3_16_0_1_1418824365076_4320" style="white-space:pre;"> </span>{</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>throw
          new Exception("Exceeds maximum order "+ maxLength);</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>}</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span></div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>while(order
          != currentAllowedOrder)</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" id="yiv3812978217yui_3_16_0_1_1418824365076_4321" style="white-space:pre;"> </span>{</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" id="yiv3812978217yui_3_16_0_1_1418824365076_4324" style="white-space:pre;"> </span>wait();</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" id="yiv3812978217yui_3_16_0_1_1418824365076_4322" style="white-space:pre;"> </span>}</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span></div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>try</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>{</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>currentAllowedOrder
          = currentAllowedOrder+1;</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>return
          callable.call();</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>}</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>finally</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>{</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>notify();</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>}</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style=""><span class="yiv3812978217" style="white-space:pre;"> </span>}</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style="">}</div>
        <div class="yiv3812978217" style=""><br clear="none" class="yiv3812978217" style="">
        </div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style="">Regards</div>
        <div class="yiv3812978217" dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2786" style="">Suman</div>
        <br clear="none">
        <div id="yiv3812978217yui_3_16_0_1_1418824365076_2724" style="font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;">
          <div id="yiv3812978217yui_3_16_0_1_1418824365076_2723" style="font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;">
            <div dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2722">
              <hr size="1"> <font id="yiv3812978217yui_3_16_0_1_1418824365076_2721" face="Arial" size="2"> <b><span style="font-weight:bold;">From:</span></b> Peter
                Levart <a rel="nofollow" shape="rect" class="yiv3812978217moz-txt-link-rfc2396E" ymailto="mailto:peter.levart@gmail.com" target="_blank" href="mailto:peter.levart@gmail.com"><peter.levart@gmail.com></a><br clear="none">
                <b><span style="font-weight:bold;">To:</span></b>
                Hanson Char <a rel="nofollow" shape="rect" class="yiv3812978217moz-txt-link-rfc2396E" ymailto="mailto:hanson.char@gmail.com" target="_blank" href="mailto:hanson.char@gmail.com"><hanson.char@gmail.com></a> <br clear="none">
                <b><span style="font-weight:bold;">Cc:</span></b>
                concurrency-interest
                <a rel="nofollow" shape="rect" class="yiv3812978217moz-txt-link-rfc2396E" ymailto="mailto:concurrency-interest@cs.oswego.edu" target="_blank" href="mailto:concurrency-interest@cs.oswego.edu"><concurrency-interest@cs.oswego.edu></a> <br clear="none">
                <b><span style="font-weight:bold;">Sent:</span></b>
                Sunday, December 14, 2014 11:01 PM<br clear="none">
                <b id="yiv3812978217yui_3_16_0_1_1418824365076_3068"><span id="yiv3812978217yui_3_16_0_1_1418824365076_3067" style="font-weight:bold;">Subject:</span></b>
                Re: [concurrency-interest] Enforcing ordered execution
                of critical sections?<br clear="none">
              </font> </div>
            <div class="yiv3812978217y_msg_container" id="yiv3812978217yui_3_16_0_1_1418824365076_2884"><br clear="none">
              <div id="yiv3812978217">
                <div id="yiv3812978217yui_3_16_0_1_1418824365076_2883"> <br clear="none">
                  <div class="yiv3812978217moz-cite-prefix" id="yiv3812978217yui_3_16_0_1_1418824365076_2893">On 12/14/2014
                    06:11 PM, Hanson Char wrote:<br clear="none">
                  </div>
                  <blockquote id="yiv3812978217yui_3_16_0_1_1418824365076_2888" type="cite">
                    <div dir="ltr" id="yiv3812978217yui_3_16_0_1_1418824365076_2887">Hi
                      Peter,
                      <div id="yiv3812978217yui_3_16_0_1_1418824365076_2894"><br clear="none">
                      </div>
                      <div id="yiv3812978217yui_3_16_0_1_1418824365076_2895">Thanks
                        for this proposed idea of using LockSupport.
                        This begs the question: which one would you
                        choose if you had all three (correct)
                        implementation available?  (Semaphore,
                        CountDownLatch, or LockSupport)?</div>
                      <div id="yiv3812978217yui_3_16_0_1_1418824365076_2886"><br clear="none">
                      </div>
                      <div id="yiv3812978217yui_3_16_0_1_1418824365076_2896">Regards,</div>
                      <div id="yiv3812978217yui_3_16_0_1_1418824365076_2897">Hanson</div>
                    </div>
                  </blockquote>
                  <br clear="none">
                  The Semaphore/CountDownLatch variants are equivalent
                  if you don't need re-use. So any would do. They lack
                  invalid-use detection. What happens if they are not
                  used as intended? Semaphore variant acts differently
                  than CountDownLatch variant. The low-level variant I
                  proposed detects invalid usage. So I would probably
                  use this one. But the low level variant is harder to
                  reason about it's correctness. I think it is correct,
                  but you should show it to somebody else to confirm
                  this.<br clear="none">
                  <br clear="none">
                  Another question is whether you actually need this
                  kind of synchronizer. Maybe if you explained what you
                  are trying to achieve, somebody could have an idea how
                  to do that even more elegantly...<br clear="none">
                  <br clear="none">
                  Regards, Peter
                  <div class="yiv3812978217qtdSeparateBR"><br clear="none">
                    <br clear="none">
                  </div>
                  <div class="yiv3812978217yqt9177439920" id="yiv3812978217yqtfd99782"><br clear="none">
                    <br clear="none">
                    <blockquote id="yiv3812978217yui_3_16_0_1_1418824365076_2910" type="cite">
                      <div class="yiv3812978217gmail_extra" id="yiv3812978217yui_3_16_0_1_1418824365076_2909"><br clear="none">
                        <div class="yiv3812978217gmail_quote" id="yiv3812978217yui_3_16_0_1_1418824365076_2908">On Sun,
                          Dec 14, 2014 at 9:01 AM, Peter Levart <span dir="ltr"><<a rel="nofollow" shape="rect" ymailto="mailto:peter.levart@gmail.com" target="_blank" href="mailto:peter.levart@gmail.com">peter.levart@gmail.com</a>></span>
                          wrote:
                          <blockquote class="yiv3812978217gmail_quote" id="yiv3812978217yui_3_16_0_1_1418824365076_2907" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
                            <div id="yiv3812978217yui_3_16_0_1_1418824365076_2906" style="background-color:rgb(255,255,255);color:rgb(0,0,0);"> Hi
                              Hanson,<br clear="none">
                              <br clear="none">
                              This one is more low-level, but catches
                              some invalid usages and is more
                              resource-friendly:<br clear="none">
                              <br clear="none">
                              <br clear="none">
                              public class OrderedExecutor {<br clear="none">
                              <br clear="none">
                                  public <T> T
                              execCriticalSectionInOrder(<br clear="none">
                                      final int order,<br clear="none">
                                      final Supplier<T>
                              criticalSection<span class="yiv3812978217" id="yiv3812978217yui_3_16_0_1_1418824365076_3633"><br clear="none">
                                    ) throws InterruptedException {<br clear="none">
                                        if (order < 0) {<br clear="none">
                              </span>             throw new
                              IllegalArgumentException("'order' should
                              be >= 0");<br clear="none">
                                      }<br clear="none">
                                      if (order > 0) {<br clear="none">
                                          waitForDone(order - 1);<br clear="none">
                                      }<br clear="none">
                                      try {<br clear="none">
                                          return criticalSection.get();<br clear="none">
                                      } finally {<br clear="none">
                                          notifyDone(order);<br clear="none">
                                      }<br clear="none">
                                  }<br clear="none">
                              <br clear="none">
                                  private static final Object DONE = new
                              Object();<br clear="none">
                                  private final
                              ConcurrentMap<Integer, Object>
                              signals = new ConcurrentHashMap<>();<br clear="none">
                              <br clear="none">
                                  private void waitForDone(int order)
                              throws InterruptedException {<br clear="none">
                                      Object sig =
                              signals.putIfAbsent(order,
                              Thread.currentThread());<br clear="none">
                                      if (sig != null && sig !=
                              DONE) {<br clear="none">
                                          throw new
                              IllegalStateException();<br clear="none">
                                      }<br clear="none">
                                      while (sig != DONE) {<br clear="none">
                                          LockSupport.park();<br clear="none">
                                          if (Thread.interrupted()) {<br clear="none">
                                              throw new
                              InterruptedException();<br clear="none">
                                          }<br clear="none">
                                          sig = signals.get(order);<br clear="none">
                                      }<br clear="none">
                                  }<br clear="none">
                              <br clear="none">
                                  private void notifyDone(int order) {<br clear="none">
                                      Object sig =
                              signals.putIfAbsent(order, DONE);<br clear="none">
                                      if (sig instanceof Thread) {<br clear="none">
                                          if (!signals.replace(order,
                              sig, DONE)) {<br clear="none">
                                              throw new
                              IllegalStateException();<br clear="none">
                                          }<br clear="none">
                                          LockSupport.unpark((Thread)
                              sig);<br clear="none">
                                      } else if (sig != null) {<br clear="none">
                                          throw new
                              IllegalStateException();<br clear="none">
                                      }<br clear="none">
                                  }<br clear="none">
                              }<br clear="none">
                              <br clear="none">
                              <br clear="none">
                              Regards, Peter
                              <div id="yiv3812978217yui_3_16_0_1_1418824365076_3226">
                                <div class="yiv3812978217h5" id="yiv3812978217yui_3_16_0_1_1418824365076_3225"><br clear="none">
                                  <br clear="none">
                                  <div>On 12/14/2014 05:08 PM, Peter
                                    Levart wrote:<br clear="none">
                                  </div>
                                  <blockquote id="yiv3812978217yui_3_16_0_1_1418824365076_3224" type="cite">
                                    <br clear="none">
                                    <div>On 12/14/2014 04:20 PM, Hanson
                                      Char wrote:<br clear="none">
                                    </div>
                                    <blockquote type="cite">
                                      <div dir="ltr">Hi Peter,
                                        <div><br clear="none">
                                        </div>
                                        <div>Thanks for the suggestion,
                                          and sorry about not being
                                          clear about one important
                                          detail: "n" is not known a
                                          priori when constructing an
                                          OrderedExecutor.  Does this
                                          mean the use of
                                           CountDownLatch is ruled out?</div>
                                      </div>
                                    </blockquote>
                                    <br clear="none">
                                    If you know at least the upper bound
                                    of 'n', it can be used with such
                                    'n'. Otherwise something that
                                    dynamically re-sizes the array could
                                    be devised. Or you could simply use
                                    a ConcurrentHashMap instead of array
                                    where keys are 'order' values:<br clear="none">
                                    <br clear="none">
                                    <br clear="none">
                                    public class
                                    OrderedExecutor<T> {<br clear="none">
                                    <br clear="none">
                                        private final
                                    ConcurrentMap<Integer,
                                    CountDownLatch> latches = new
                                    ConcurrentHashMap<>();<br clear="none">
                                    <br clear="none">
                                        public T
                                    execCriticalSectionInOrder(final int
                                    order,<br clear="none">
                                                                           
                                    final Supplier<T>
                                    criticalSection) throws
                                    InterruptedException {<br clear="none">
                                            if (order > 0) {<br clear="none">
                                               
                                    latches.computeIfAbsent(order - 1, o
                                    -> new
                                    CountDownLatch(1)).await();<br clear="none">
                                            }<br clear="none">
                                            try {<br clear="none">
                                                return
                                    criticalSection.get();<br clear="none">
                                            } finally {<br clear="none">
                                               
                                    latches.computeIfAbsent(order, o
                                    -> new
                                    CountDownLatch(1)).countDown();<br clear="none">
                                            }<br clear="none">
                                        }<br clear="none">
                                    }<br clear="none">
                                    <br clear="none">
                                    <br clear="none">
                                    Regards, Peter<br clear="none">
                                    <br clear="none">
                                    <blockquote type="cite">
                                      <div dir="ltr">
                                        <div><br clear="none">
                                        </div>
                                        <div>You guessed right: it's a
                                          one-shot object for a
                                          particular OrderedExecutor
                                          instance, and "order" must be
                                          called indeed at most once.</div>
                                        <div><br clear="none">
                                        </div>
                                        <div>Regards,</div>
                                        <div>Hanson</div>
                                        <div class="yiv3812978217gmail_extra"><br clear="none">
                                          <div class="yiv3812978217gmail_quote">On
                                            Sun, Dec 14, 2014 at 2:21
                                            AM, Peter Levart <span dir="ltr"><<a rel="nofollow" shape="rect" ymailto="mailto:peter.levart@gmail.com" target="_blank" href="mailto:peter.levart@gmail.com">peter.levart@gmail.com</a>></span>
                                            wrote:
                                            <blockquote class="yiv3812978217gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
                                              <div style="background-color:rgb(255,255,255);color:rgb(0,0,0);">
                                                Hi Hanson,<br clear="none">
                                                <br clear="none">
                                                I don't think anything
                                                like that readily exists
                                                in java.lang.concurrent,
                                                but what you describe
                                                should be possible to
                                                achieve with composition
                                                of existing primitives.
                                                You haven't given any
                                                additional hints to what
                                                your OrderedExecutor
                                                should behave like.
                                                Should it be a one-shot
                                                object (like
                                                CountDownLatch) or a
                                                re-usable one (like
                                                CyclicBarrier)? Will
                                                execCriticalSectionInOrder()
                                                for a particular
                                                OrderedExecutor instance
                                                and 'order' value be
                                                called at most once? If
                                                yes (and I think that
                                                only a one-shot object
                                                makes sense here), an
                                                array of
                                                CountDownLatch(es) could
                                                be used:<br clear="none">
                                                <br clear="none">
                                                public class
                                                OrderedExecutor<T>
                                                {<br clear="none">
                                                    private final
                                                CountDownLatch[]
                                                latches;<br clear="none">
                                                <br clear="none">
                                                    public
                                                OrderedExecutor(int n) {<br clear="none">
                                                        if (n < 1)
                                                throw new
                                                IllegalArgumentException("'n'
                                                should be >= 1");<br clear="none">
                                                        latches = new
                                                CountDownLatch[n - 1];<br clear="none">
                                                        for (int i = 0;
                                                i < latches.length;
                                                i++) {<br clear="none">
                                                            latches[i] =
                                                new CountDownLatch(1);<span><br clear="none">
                                                          }<br clear="none">
                                                      }<br clear="none">
                                                  <br clear="none">
                                                      public T
                                                  execCriticalSectionInOrder(final
                                                  int order,<br clear="none">
                                                </span>
                                                                                       
                                                final Supplier<T>
                                                criticalSection) throws
                                                InterruptedException {<br clear="none">
                                                        if (order < 0
                                                || order >
                                                latches.length)<br clear="none">
                                                            throw new
                                                IllegalArgumentException("'order'
                                                should be [0..." +
                                                latches.length + "]");<br clear="none">
                                                        if (order >
                                                0) {<br clear="none">
                                                           
                                                latches[order -
                                                1].await();<br clear="none">
                                                        }<br clear="none">
                                                        try {<br clear="none">
                                                            return
                                                criticalSection.get();<br clear="none">
                                                        } finally {<br clear="none">
                                                            if (order
                                                < latches.length) {<br clear="none">
                                                               
                                                latches[order].countDown();<br clear="none">
                                                            }<br clear="none">
                                                        }<br clear="none">
                                                    }<br clear="none">
                                                }<br clear="none">
                                                <br clear="none">
                                                <br clear="none">
                                                Regards, Peter
                                                <div>
                                                  <div><br clear="none">
                                                    <br clear="none">
                                                    <div>On 12/14/2014
                                                      05:26 AM, Hanson
                                                      Char wrote:<br clear="none">
                                                    </div>
                                                  </div>
                                                </div>
                                                <blockquote type="cite">
                                                  <div>
                                                    <div>Hi,
                                                      I am looking for a
                                                      construct that can
                                                      be used to
                                                      efficiently
                                                      enforce
                                                      ordered execution
                                                      of multiple
                                                      critical sections,
                                                      each calling from
                                                      a
                                                      different thread.
                                                      The calling
                                                      threads may run in
                                                      parallel and may
                                                      call
                                                      the execution
                                                      method out of
                                                      order. The
                                                      perceived
                                                      construct would
                                                      therefore be
                                                      responsible for
                                                      re-ordering the
                                                      execution of those
                                                      threads, so that
                                                      their critical
                                                      sections (and only
                                                      the critical
                                                      section) will be
                                                      executed in order.
                                                      Would something
                                                      like the following
                                                      API already exist?
                                                      /** * Used to
                                                      enforce ordered
                                                      execution of
                                                      critical sections
                                                      calling from
                                                      multiple *
                                                      threads, parking
                                                      and unparking the
                                                      threads as
                                                      necessary. */
                                                      public class
                                                      OrderedExecutor<T>
                                                      { /** * Executes a
                                                      critical section
                                                      at most once with
                                                      the given order,
                                                      parking * and
                                                      unparking the
                                                      current thread as
                                                      necessary so that
                                                      all critical *
                                                      sections executed
                                                      by different
                                                      threads using this
                                                      executor take
                                                      place in * the
                                                      order from 1 to n
                                                      consecutively. */
                                                      public T
                                                      execCriticalSectionInOrder(</div>
                                                  </div>
                                                  final int order, final
                                                  Callable<T>
                                                  criticalSection)
                                                  throws
                                                  InterruptedException;
                                                  }
                                                  Regards,
                                                  Hanson
                                                  _______________________________________________
Concurrency-interest
                                                  mailing list
                                                  <a rel="nofollow" shape="rect" ymailto="mailto:Concurrency-interest@cs.oswego.edu" target="_blank" href="mailto:Concurrency-interest@cs.oswego.edu">Concurrency-interest@cs.oswego.edu</a>
                                                  <a rel="nofollow" shape="rect" target="_blank" href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a>
                                                </blockquote>
                                                <br clear="none">
                                              </div>
                                            </blockquote>
                                          </div>
                                        </div>
                                      </div>
                                    </blockquote>
                                    <br clear="none">
                                  </blockquote>
                                  <br clear="none">
                                </div>
                              </div>
                            </div>
                          </blockquote>
                        </div>
                      </div>
                    </blockquote>
                    <br clear="none">
                  </div>
                </div>
              </div>
              <br clear="none">
              <div class="yiv3812978217yqt9177439920" id="yiv3812978217yqtfd70219">_______________________________________________<br clear="none">
                Concurrency-interest mailing list<br clear="none">
                <a rel="nofollow" shape="rect" ymailto="mailto:Concurrency-interest@cs.oswego.edu" target="_blank" href="mailto:Concurrency-interest@cs.oswego.edu">Concurrency-interest@cs.oswego.edu</a><br clear="none">
                <a rel="nofollow" shape="rect" target="_blank" href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a><br clear="none">
              </div>
              <br clear="none">
              <br clear="none">
            </div>
          </div>
        </div>
      </div>
      <br clear="none">
      <fieldset class="yiv3812978217mimeAttachmentHeader"></fieldset>
      <br clear="none">
      <pre>_______________________________________________
Concurrency-interest mailing list
<a rel="nofollow" shape="rect" class="yiv3812978217moz-txt-link-abbreviated" ymailto="mailto:Concurrency-interest@cs.oswego.edu" target="_blank" href="mailto:Concurrency-interest@cs.oswego.edu">Concurrency-interest@cs.oswego.edu</a>
<a rel="nofollow" shape="rect" class="yiv3812978217moz-txt-link-freetext" target="_blank" href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a>
</pre>
    </blockquote></div>
    <br clear="none">
  </div></div><br><br></div> </div> </div>  </div></body></html>