If you re-run Android Thread, it says it's already running thread.

Asked 2 years ago, Updated 2 years ago, 63 views

Hi, everyone. I'm asking you this question because I'm trying to reuse Thread on Android.

/* This is a spinning operation on the main thread. */

// ParserThread is the class that inherited the Thread class.
// Only run() was overridden by the internal method.
ParserThread thread = new ParserThread();

// Start the thread.
thread.start();

/* Workspace */
doSomething();

// Wait for the thread to exit, and if it has, run again.
thread.join();
If (!thread.isAlive()) // I added the conditions to make sure it's complete.
    Thread.start(); /* I get a Thread already started error here */

Error at last thread.start(). Thread.join() passes quickly, and if you break the if statement and check the state of the thread through thread.getState(), it says Terminated. There is no such thread in the call stack.

I checked that the thread has already been executed, but the error keeps coming up.

I don't know what the problem is.

Can't I reuse Android's Thread class at all? I can't create a new thread every time I turn the same thread. I remember that the thread structure on Linux was reusable, but it's different from this.ㅠ<

android thread

2022-09-21 15:25

1 Answers

java api - thread cannot be started more than once. It's troublesome, but the only way to start is to create a new thread instance.

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.


2022-09-21 15:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.