Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wake up RecursiveTask<V> with notify?
    primarykey
    data
    text
    <p>I cannot figure it out, how can I wake up <strong>RecursiveTasks</strong>, invoked by <strong>ForkJoinPool</strong>, when these tasks are on hold by <strong>wait</strong> method. Here is my simple example with the method <em>MainRecursionClass.resume</em> which is incorrect (does not wakes up RecursiveTasks).</p> <pre><code>public class Program { public static void main(String[] args) { Program p = new Program(); final MainRecursionClass mrc = p.new MainRecursionClass(); //Thread outputs integers to simulate work new Thread() { public void run() { mrc.doJob(); } }.start(); //Thread performs wait and notify on MainRecursionClass object p.new PauseResume(mrc).start(); } /** * * This class performs suspend and resume operations to the MainRecursionClass class object * */ private class PauseResume extends Thread { private MainRecursionClass rv; public PauseResume(MainRecursionClass rv) { this.rv = rv; } @Override public void run() { while(!isInterrupted()) { try { sleep(4000); rv.suspend(); sleep(8000); rv.resume(); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class MainRecursionClass { private boolean pause = false; private MyRecursive rv; public void doJob() { rv = new MyRecursive(0, 100000); ForkJoinPool pool = new ForkJoinPool(); pool.invoke(rv); } public void suspend() { pause = true; System.out.println("Suspended"); } /** * This method is incorrect. It should wake up all MyRecursive instances to continue their work. */ public synchronized void resume() { pause = false; notifyAll(); System.out.println("Resumed"); } private class MyRecursive extends RecursiveTask&lt;Object&gt; { private static final long serialVersionUID = 1L; private int start; private int length; private int threshold = 15; public MyRecursive(int start, int length) { super(); this.start = start; this.length = length; } protected void computeDirectly() throws Exception { for (int index = start; index &lt; start + length; index++) { //PAUSE synchronized (this) { try { while(pause) { wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } //PAUSE //some output to simulate work... System.out.println(index); Thread.sleep(1000); } } /** * Recursion */ @Override protected Object compute() { if (length &lt;= threshold) { try { computeDirectly(); } catch (Exception e) { return e; } return null; } int split = length / 2; invokeAll(new MyRecursive(start, split), new MyRecursive(start + split, length - split)); return null; } } } } </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. 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