Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generally speaking, the way to do this is to have each of the other threads periodically check a flag. Often background threads loop, waiting for work - they just have to check the flag each time they go round a loop. If they're using <code>Object.wait()</code> or something similar to be told that there's more work, the same notification should be used to indicate that the thread should stop too. (Don't just spin until you're stopped - that will suck CPU. Don't just use sleep - that will delay termination.)</p> <p>That allows all threads to terminate cleanly, releasing resources appropriately. Other options such as <code>interrupt()</code> and the deprecated <code>destroy()</code> method are much harder to control properly, IMO. (Interrupting a thread is better than hard-aborting it, but it has its own set of problems - such as the interruption is only processed at certain points anyway.)</p> <p>EDIT: In code, it would look something like:</p> <pre><code>// Client code for (Task task : tasks) { task.stop(); } // Threading code public abstract class Task implements Runnable { private volatile boolean stopped = false; public void stop() { stopped = true; } protected boolean shouldStop() { return stopped; } public abstract void run(); } </code></pre> <p>Your tasks would then subclass <code>Task</code>. You would need to make it slightly more complicated if you wanted the <code>stop()</code> method to also notify a monitor, but that's the basic idea.</p> <p>Sample task:</p> <pre><code>public class SomeTask extends Task { public void run() { while (!shouldStop()) { // Do work } } } </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