-3

I am making a program and I need one thread to stop and another to start. my problem is that if I do t1.stop() than t1.start() I get the java.lang.IllegalThreadStateException

            if (t1.isAlive() == true){
                    t1.stop();
// above I stop the thread and call another
                    t2.start();
                    System.out.println("t1 was playing");
                }else{
                    t2.stop();
                    t1.start(); 
// above I stop the other thread and want to start the first thread again, but when I run the program I get the exception i said above
                }
Cam Connor
  • 1,231
  • 2
  • 25
  • 40
  • 2
    That's not a question. – djechlin Mar 31 '13 at 05:29
  • http://stackoverflow.com/questions/1215548/is-it-legal-to-call-the-start-method-twice-on-the-same-thread/1215553#1215553 –  Mar 31 '13 at 05:51
  • 1
    -1 Search *first*. I used `IllegalThreadStateException thread start` and found no shortage of duplicates. –  Mar 31 '13 at 05:52
  • (In any case, solutions are to either create a *new* thread or provide a mechanism to "idle" the thread when it needs to be temporarily suspended - such as when a game is paused.) –  Mar 31 '13 at 06:13

2 Answers2

3

When a thread is stopped, you cannot restart it.

  1. However, you can create and start a new thread.
  2. Also, you can suspend and resume the thread.

The java primitive to suspend and resume a thread (along with stop etc) is deprecated. See this to figure how you can achieve best what you need - http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html Check how you can do the equivalent of suspend & resume

What should I use instead of Thread.suspend and Thread.resume?

As with Thread.stop, the prudent approach is to have the "target thread" poll a variable indicating the desired state of the thread (active or suspended). When the desired state is suspended, the thread waits using Object.wait. When the thread is resumed, the target thread is notified using Object.notify.

Example code is given in the same answer to help you achieve this.

Community
  • 1
  • 1
user93353
  • 13,733
  • 8
  • 60
  • 122
2

Thread#stop is deprecated and you shouldn't use it.

From the JavaDocs

stop()
Deprecated.

This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.

Threads are also non-re-entrant, or, are single use. Once the run method terminates, they can not be restarted

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366