Is it legal to call the launch method twice on the same topic?

The following code leads to java.lang.IllegalThreadStateException: Thread already started when I called the start() method a second time in the program.

 updateUI.join(); if (!updateUI.isAlive()) updateUI.start(); 

This happens when the second time updateUI.start() called. I went through it several times, and the thread is called and terminated before completion, before clicking updateUI.start() .

Calling updateUI.run() avoids the error, but causes the thread to start in the user interface thread (the calling thread, as indicated in other posts on SO), which is not what I want.

Is it possible to run Thread only once? If so, what do I do if I want to start the thread again? This particular thread does some calculations in the background, if I do not do this in the stream than in the UI thread, and the user has an unreasonably long wait.

+78
java android multithreading
Aug 01 '09 at 1:15
source share
10 answers

From the Java API Specification for Thread.start Method:

You cannot start a thread more than once. In particular, the thread cannot be restarted and completed execution.

Further

It produces:
IllegalThreadStateException - if the thread is already running.

So, yes, Thread can only be run once.

If so, what will I do if I want to start the thread again?

If a Thread needs to be started more than once, then you need to create a new instance of Thread and call start on it.

+100
Aug 01 '09 at 1:18
source share

That's right. From the documentation :

You cannot start a thread more than once. In particular, the thread cannot be restarted and completed execution.

In terms of what you can do for recalculations, it seems that you can use the SwingUtilities invokeLater method . You are already experimenting with calling run() directly, that is, you are already thinking about using Runnable and not about raw Thread . Try using the invokeLater method only for the Runnable task and see if it fits your mental pattern better.

Here is an example from the documentation:

  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."); 

If you replace this println call with your calculation, this may be exactly what you need.

EDIT: after the comment, I did not notice the Android tag in the original post. The equivalent of invokeLater in Android is Handler.post(Runnable) . From my javadoc:

 /** * 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. */ 

So, in the Android world, you can use the same example as above, replacing Swingutilities.invokeLater with the corresponding entry on Handler .

+13
Aug 01 '09 at 1:20
source share

The answer you just received explains why you should not do what you are doing. Here are some options for solving your current problem.

This particular thread is computation in the background, if I do not do this in the thread, than it does in the user interface thread, and the user has an unreasonably long wait.

Reset your own thread and use AsyncTask .

Or create a fresh stream when you need it.

Or configure your thread to work from a work queue (e.g. LinkedBlockingQueue ) rather than restarting the thread.

+3
Aug 01 '09 at 1:19
source share

No , we wonโ€™t be able to run Thread again, as throwtimeException java.lang.IllegalThreadStateException will throw it. >

The reason is that the run () method is executed by Thread, it goes into a dead state.

Let's take an example- Thinking again about resuming a thread and calling the start () method on it (which is internally going to call the run () method) for us is like asking a dead person to wake up and run. Because, after completing his life, a person goes into a dead state.

 public class MyClass implements Runnable{ @Override public void run() { System.out.println("in run() method, method completed."); } public static void main(String[] args) { MyClass obj=new MyClass(); Thread thread1=new Thread(obj,"Thread-1"); thread1.start(); thread1.start(); //will throw java.lang.IllegalThreadStateException at runtime } } 

/ * OUTPUT in the run () method, the method is complete. Exception in thread "main" java.lang.IllegalThreadStateException in java.lang.Thread.start (Unknown source) * /

check this

+3
Mar 03 '16 at 10:00
source share

What you need to do is create a Runnable and wrap it with a new thread every time you want to run Runnable. That would be very ugly, but you can wrap the thread in another thread to run the code for it again, but thatโ€™s what you really need.

+2
Aug 01 '09 at 18:21
source share

As you said, a thread cannot be started more than once.

Straight from the mouth of the horse: Java API Spec

You cannot start a thread more than once. In particular, the thread cannot be restarted and completed execution.

If you need to restart everything that happens in your thread, you will need to create a new thread and start it.

+1
Aug 01 '09 at 1:19
source share

Reusing a thread is an illegal action in the Java API. However, you can port it to a runnable implementation and run this instance again.

0
Aug 14 '13 at 9:28
source share

Yes, we cannot start an already running thread. It will throw an IllegalThreadStateException at run time - if the thread is already running.

What to do if you really need to start a thread: Option 1) If you need to start a thread more than once, then you need to create a new instance of Thread and start with it.

0
Jul 25 '17 at 9:30
source share

Is it possible to run Thread only once?

Yes. You can run it exactly once.

If so, what can I do if I want to start the thread again? This particular thread does some calculations in the background, if I do not do this in the stream than in the UI thread, and the user has an unreasonably long wait.

Do not run Thread again. Instead, create a Runnable and publish it to Handler of HandlerThread . You can submit multiple Runnable objects. If you want to send data back to the user interface thread using the Runnable run() method, enter Message on the Handler of the user interface thread and the handleMessage process

Refer to this post, for example, to the code:

Android: toast in the stream

0
Aug 30 '17 at 7:12
source share

It would be very ugly to do, but you can wrap the thread with another thread to run the code for it again, but thatโ€™s what you really need.

I had to fix a resource leak caused by the programmer who created Thread, but instead of running it (), he called the run () method - directly. Therefore, avoid this if you really do not know what side effects it causes.

-one
Nov 15 '11 at 13:16
source share



All Articles