Stop thread and again start giving IllegalThreadStateException in blackberry

前端 未结 2 1356
轮回少年
轮回少年 2020-12-06 06:10

I am getting IllegalThreadStateException exception when using following code: I have already started this thread once(by using thread.start()) and

2条回答
  •  鱼传尺愫
    2020-12-06 06:26

    Thread objects are only meant to be started once. If you need to stop/interrupt a Thread, and then want to start it again, you should create a new instance, and call start() on it:

    thread.interrupt();  // if you need to make sure thread's run() method stops ASAP
    thread = new MyThreadSubclass();
    thread.start();
    

    From the API docs

    IllegalThreadStateException - if the thread was already started.

    I know it's not 100% clear that you can't call start() again, even if you previously called interrupt(), but that's the way it works.

    If you look at the API docs for standard Java, this issue is more clear.

提交回复
热议问题