Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Exactly right. <a href="http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#start%28%29" rel="noreferrer">From the documentation</a>:</p> <blockquote> <p>It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.</p> </blockquote> <p>In terms of what you can do for repeated computation, it seems as if you could use <a href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29" rel="noreferrer">SwingUtilities invokeLater method</a>. You are already experimenting with calling <code>run()</code> directly, meaning you're already thinking about using a <code>Runnable</code> rather than a raw <code>Thread</code>. Try using the <code>invokeLater</code> method on just the <code>Runnable</code> task and see if that fits your mental pattern a little better.</p> <p>Here is the example from the documentation:</p> <pre><code> Runnable doHelloWorld = new Runnable() { public void run() { // Put your UI update computations in here. // BTW - remember to restrict Swing calls to the AWT Event thread. System.out.println("Hello World on " + Thread.currentThread()); } }; SwingUtilities.invokeLater(doHelloWorld); System.out.println("This might well be displayed before the other message."); </code></pre> <p>If you replace that <code>println</code> call with your computation, it might just be exactly what you need.</p> <p>EDIT: following up on the comment, I hadn't noticed the Android tag in the original post. The equivalent to invokeLater in the Android work is <code>Handler.post(Runnable)</code>. From its javadoc:</p> <pre><code>/** * Causes the Runnable r to be added to the message queue. * The runnable will be run on the thread to which this handler is * attached. * * @param r The Runnable that will be executed. * * @return Returns true if the Runnable was successfully placed in to the * message queue. Returns false on failure, usually because the * looper processing the message queue is exiting. */ </code></pre> <p>So, in the Android world, you can use the same example as above, replacing the <code>Swingutilities.invokeLater</code> with the appropriate post to a <code>Handler</code>.</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. 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