I have Thrad and Handler:
Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
//do somethink
}
};
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//do somethink
msg.obj = 1;
handler.sendMessage(msg);
thread.interrupt();
}
});
When app start, at first time thread.start(); all work fine. But when I try start thread.start(); second time from button I have:
E/MessageQueue-JNI﹕ java.lang.IllegalThreadStateException: Thread already started.
You should check state of that thread before starting it.
if (thread.getState() == Thread.State.NEW)
{
thread.start();
}
Its not a good Idea to start a Thread more then once. You have to check Whether a Thread is already started or not. if Thread not started yet
if(!thread.isAlive()){
thread.start();
}
The Better Idea is to Create new Thread instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With