Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Working directly with <code>synchronized</code>, <code>wait()</code>, and <code>notify()</code> is definitely tricky.</p> <p>Fortunately the <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html" rel="nofollow noreferrer">Java Concurrency API</a> provides some excellent control objects for this sort of thing that are much more intuitive. In particular, look at <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/CyclicBarrier.html" rel="nofollow noreferrer"><code>CyclicBarrier</code></a> and <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html" rel="nofollow noreferrer"><code>CountDownLatch</code></a>; one of them almost certainly will be what you're looking for.</p> <p>You may also find a <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html" rel="nofollow noreferrer"><code>ThreadPoolExecutor</code></a> to be handy for this situation.</p> <p>Here's a simple example / conversion of your snippet that produces the following output (without deadlock, of course):</p> <blockquote> <p>Read line: Line 1<br> Waiting for work to be complete on line: Line 1<br> Working on line: Line 1<br> Working on line: Line 1<br> Read line: Line 2<br> Waiting for work to be complete on line: Line 2<br> Working on line: Line 2<br> Working on line: Line 2<br> Read line: Line 3<br> Waiting for work to be complete on line: Line 3<br> Working on line: Line 3<br> Working on line: Line 3<br> All work complete!</p> </blockquote> <pre><code>public class Runner { public static void main(String args[]) { Runner r = new Runner(); try { r.dowork(); } catch (IOException e) { // handle e.printStackTrace(); } } CyclicBarrier barrier; ExecutorService executor; private int totalWorkers = 2; public Runner() { this.barrier = new CyclicBarrier(this.totalWorkers + 1); this.executor = Executors.newFixedThreadPool(this.totalWorkers); } public synchronized void dowork() throws IOException { //&lt;code for opening a file here, other setup here, etc&gt; //BufferedReader reader = null; //String line; final Worker worker = new Worker(); for(String line : new String[]{"Line 1", "Line 2", "Line 3"}) //while ((line = reader.readLine()) != null) { System.out.println("Read line: " + line); //&lt;a large amount of processing on 'line'&gt; for(int c = 0; c &lt; this.totalWorkers; c++) { final String curLine = line; this.executor.submit(new Runnable() { public void run() { worker.doWork(curLine); } }); } try { System.out.println("Waiting for work to be complete on line: " + line); this.barrier.await(); } catch (InterruptedException e) { // handle e.printStackTrace(); } catch (BrokenBarrierException e) { // handle e.printStackTrace(); } } System.out.println("All work complete!"); } class Worker { public void doWork(String line) { //&lt;do work with this.data here&gt; System.out.println("Working on line: " + line); try { Runner.this.barrier.await(); } catch (InterruptedException e) { // handle e.printStackTrace(); } catch (BrokenBarrierException e) { // handle e.printStackTrace(); } } } } </code></pre>
    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