Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no unique answer to your question. It depends on what the thread does. First of all, how do you plan to stop that thread?</p> <ol> <li>If it elaborates some stuff, and this stuff is made of "tokens" such as single "tasks" to do, and the thread grabs them from a <code>BlockingQueue</code> for example, then you can stop the thread with a so-called "poison pill", like a mock task that the thread reads as: "Hey man, I have to shut down".</li> <li>If the thread does something which cannot be "divided" into "steps" then you should actually tell us how do you think it can be shut down. If it blocks on <code>read()</code>s on a socket, you can only close the socket to unblock it.</li> </ol> <p>Please note <code>interrupt()</code> is not meant to stop a <code>Thread</code> in normal circumstances, and if you <code>interrupt()</code> a thread, in most cases it'll just go on doing its stuff. In 99.9% of cases, it is just bad design to stop a thread with <code>interrupt()</code>.</p> <p><strong>Anyway, to stop it after 5 seconds, just set a <code>Timer</code> which does so. Or better <code>join</code> it with a timeout of 5 seconds, and after that, stop it. Problem is "how" to stop it. So please tell me how do you think the thread should be stopped so that I can help you in better way.</strong></p> <p>Tell me what the thread does.</p> <p>EDIT: in reply to your comment, just an example</p> <pre><code>class MyHTTPTransaction extends Thread { public MyHTTPTransaction(.....) { /* ... */ } public void run() { /* do the stuff with the connection */ } public void abortNow() { /* roughly close the connection ignoring exceptions */ } } </code></pre> <p>and then</p> <pre><code>MyHTTPTransaction thread = new MyHTTPTransaction(.....); thread.start(); thread.join(5000); /* check if the thread has completed or is still hanging. If it's hanging: */ thread.abortNow(); /* else tell the user everything went fine */ </code></pre> <p>:)</p> <p>You can also set a <code>Timer</code>, or use <code>Condition</code> on <code>Lock</code>s because I bet there can be some race condition.</p> <p>Cheers.</p>
    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.
 

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