Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For what I interpret from your question, since is not clearly stated, is that you have several downloader threads and want to synchronize them. In that case you need to use an external structure such as a <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html" rel="nofollow">CountdownLatch</a>. A CountdownLatch is a "synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes." Think of it as a counter that decreases every time a downloader thread finishes, when the countdown reaches 0 then all threads should be done with their work.</p> <p>Let's see some code taken directly from the official javadoc of CountdownLatch:</p> <pre><code> CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(N); for (int i = 0; i &lt; N; ++i) // create and start threads new Thread(new Worker(startSignal, doneSignal)).start(); startSignal.countDown(); // Threads are waiting for startSignal's counter to reach 0 and start their work, more or less at the same time doneSignal.await(); // The current thread will sleep at this point until all worker threads have each one called doneSignal.countDown() and the countdown reaches 0 joinDownloadedPieces(); // At this point is safe to assume that your threads have finished and you can go on with anything else </code></pre> <p>Your worker Runnable/Thread class would look something like this:</p> <pre><code>public void run() { startSignal.await(); // Wait until given the go downloadSomething(); doneSignal.countDown(); // Notify that we are done here } </code></pre> <p>I say is safe to assume since you will have to deal with InterruptedExceptions that can wake your thread while waiting, before all of them finish. For more information I recommend you take a look at the <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/package-summary.html" rel="nofollow">java.util.concurrent</a> package. It's IMHO one of the best gems of Java and the documentation is thorough and often more than enough to understand the function of each structure. </p>
    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. This table or related slice is empty.
    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