Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Callable Threads as Daemon
    text
    copied!<p>How can I make Callable thread as daemon thread?</p> <p>Here is what I am trying. I am trying to execute a set of threads of which one of them does not complete and goes into infinite loop. What it does is the main thread of the program does not terminate even though all the code statements are executed. The main thread goes into suspended mode after that.</p> <p>Here is the code snippet for the same.</p> <pre><code>public class MyThread implements Callable&lt;String&gt; { private int value; public MyThread(int value) { this.value = value; } @Override public String call() throws Exception { //Thread.currentThread().setDaemon(true); System.out.println("Executing - " + value); if (value == 4) { for (; ; ); } return value + ""; } } </code></pre> <p>Main Program</p> <pre><code>public class ExecutorMain { public static String testing() { ExecutorService executor = null; List&lt;Future&lt;String&gt;&gt; result = null; String parsedValue = null; try { executor = Executors.newSingleThreadExecutor(); List&lt;MyThread&gt; threads = new ArrayList&lt;MyThread&gt;(); for (int i = 1; i &lt; 10; i++) { MyThread obj = new MyThread(i); threads.add(obj); } result = executor.invokeAll(threads, Long.valueOf("4000"), TimeUnit.MILLISECONDS); //result = executor.invokeAll(threads); for (Future&lt;String&gt; f : result) { try { parsedValue = f.get(); System.out.println("Return Value - " + parsedValue); } catch (CancellationException e) { System.out.println("Cancelled"); parsedValue = ""; f.cancel(true); } } executor.shutdownNow(); } catch (Exception e) { System.out.println("Exception while running threads"); e.printStackTrace(); } finally { List executedThreads = executor.shutdownNow(); System.out.println(executedThreads); for (Object o : executedThreads) { System.out.println(o.getClass()); } } System.out.println("Exiting...."); //System.exit(1); return ""; } public static void main(String[] args) { testing(); } } </code></pre> <p>What I got to understand from my earlier question about <a href="https://stackoverflow.com/questions/10029076/dangling-threads-in-java">Dangling threads in Java</a> is that I have to make my threads as daemon threads.</p>
 

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