Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html" rel="nofollow"><code>CountDownLatch</code></a>. Initialize it before any of the threads run with the count to one. Your code will then look something like this:</p> <pre><code>class Test { static CountDownLatch latch = new CountDownLatch(1); public static void t1() { //This method will be called in a Thread called T2 /*This method must wait for flag to become 1. As soon it becomes 1 this must return. However it should wait for maximum n seconds. After that even if flag is 0 it must return.*/ latch.await(1L,TimeUnit.SECONDS); //your remaining logic } public static void m1() { //your logic latch.countDown(); } } </code></pre> <p>A <code>CountDownLatch</code> is a somewhat modified, enhanced (e.g. timeout option), and arguably easier-to-understand implementation of a <a href="https://en.wikipedia.org/wiki/Semaphore_%28programming%29" rel="nofollow">Semaphore</a> - one of the basic structures used widely for thread synchronization across a number of languages (not only Java). Note the following when comparing the Wikipedia reference to the Java implementation:</p> <ul> <li><code>P</code>/<code>wait()</code> corresponds to <code>await()</code>, with the difference that <code>await()</code> doesn't change the <code>count</code> value.</li> <li><code>V</code>/<code>signal()</code> corresponds to <code>countDown()</code>, with the difference that <code>countDown()</code> counts <strong>down</strong>, not up (obviously).</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload