Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could make use of the java.utils.concurrent.Executors.newFixedThreadPool(int nThreads) method, then call the invokeAll method (could make use of the one with the timout too to avoid the infinite loop). </p> <p>You would change your loop to add a class that is Callable that takes the "observer" and the "this" and then call the update method in the "call" method.</p> <p><a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html" rel="nofollow noreferrer">Take a look at this package for more info</a>.</p> <p>This is a quick and dirty implementation of what I was talking about:</p> <pre><code>import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Main { private Main() { } public static void main(final String[] argv) { final Watched watched; final List&lt;Watcher&gt; watchers; watched = new Watched(); watchers = makeWatchers(watched, 10); watched.notifyWatchers(9); } private static List&lt;Watcher&gt; makeWatchers(final Watched watched, final int count) { final List&lt;Watcher&gt; watchers; watchers = new ArrayList&lt;Watcher&gt;(count); for(int i = 0; i &lt; count; i++) { final Watcher watcher; watcher = new Watcher(i + 1); watched.addWatcher(watcher); watchers.add(watcher); } return (watchers); } } class Watched { private final List&lt;Watcher&gt; watchers; { watchers = new ArrayList&lt;Watcher&gt;(); } public void addWatcher(final Watcher watcher) { watchers.add(watcher); } public void notifyWatchers(final int seconds) { final List&lt;Watcher&gt; currentWatchers; final List&lt;WatcherCallable&gt; callables; final ExecutorService service; currentWatchers = new CopyOnWriteArrayList&lt;Watcher&gt;(watchers); callables = new ArrayList&lt;WatcherCallable&gt;(currentWatchers.size()); for(final Watcher watcher : currentWatchers) { final WatcherCallable callable; callable = new WatcherCallable(watcher); callables.add(callable); } service = Executors.newFixedThreadPool(callables.size()); try { final boolean value; service.invokeAll(callables, seconds, TimeUnit.SECONDS); value = service.awaitTermination(seconds, TimeUnit.SECONDS); System.out.println("done: " + value); } catch (InterruptedException ex) { } service.shutdown(); System.out.println("leaving"); } private class WatcherCallable implements Callable&lt;Void&gt; { private final Watcher watcher; WatcherCallable(final Watcher w) { watcher = w; } public Void call() { watcher.update(Watched.this); return (null); } } } class Watcher { private final int value; Watcher(final int val) { value = val; } public void update(final Watched watched) { try { Thread.sleep(value * 1000); } catch (InterruptedException ex) { System.out.println(value + "interupted"); } System.out.println(value + " done"); } } </code></pre>
 

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