I have the following thread:
public void start() {
isRunning = true;
if (mainThread == null) {
mainThread = new Thread(this);
mainThread.setPriority(Thread.MAX_PRIORITY);
}
if (!mainThread.isAlive()) {
try {
mainThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
At some point I want to stop it's operation:
public void stop() {
isRunning = false;
System.gc();
}
When calling start()
again the following exception is thrown:
java.lang.IllegalThreadStateException
Pointing the mainThread.start()
line of code.
What is the best way to start/stop a thread? how can I make this thread reusable?
Thanks!