1

Possible Duplicate:
How to start/stop/restart a thread in Java?

1.If you call start() on a running thread it's an error 2.If you call start() on a stopped thread nothing happens.

What's the reasoning behind not supporting restarts over the same object?

Community
  • 1
  • 1
Inder Singh
  • 319
  • 1
  • 5
  • 12
  • 1
    Answered here: http://stackoverflow.com/questions/1881714/how-to-start-stop-restart-a-thread-in-java – Oscar Castiblanco Apr 06 '12 at 10:52
  • It's not a dupe. The other questions asks "how restarting a thread can be done". This question asks "why it cannot be done". Though, "why the designers have chosen..." questions tend to be not-constructive – amit Apr 06 '12 at 10:57
  • Because computers have jump instructions and so can execute loops. If developers want a thread to execute some code more than once, they usually choose to put it in a loop, wait at the top on some synchronization object and then signal a run when it is required - effectively a 'restart' of the main body of the code. A 'restart' would mean another thread state, 'non-existent', 'running' and 'sort of stopped but don't destroy yet just in case it's needed again'. – Martin James Apr 06 '12 at 11:34

2 Answers2

1

I think the designers did it because OS-level threads usually behave in this way - they are created, they may run, then they are destroyed and the operating system performs cleanup. So probably Java designers wanted the thread notion in Java to be close to what a thread is in most operating systems. The methods start() and stop() are not meant to pause the thread (we have synchronization for that), only for letting it run and destroying the object. Maybe the names are a bit confusing. Note that stop() is deprecated and should not be used, so if we eliminate stop(), the name start() is not as confusing any more.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
  • Folks,the intent behind my query was to understand the technical ideology behind it. Are there any technical limitations or it's more of a design choice. – Inder Singh Apr 06 '12 at 14:08
0

Becouse of the idea behind Thread.run() method. The idea is that every thread has lifecycle and if it expires(finish the run() method block) the thread becomes dead. If you want to to stop thread for a partition of time and then run it again common way is to implement Runnable interface(or extend Thread class) and got a boolean flag inside. Here is a simple code :

public class MyThread implements Runnable {
    private Thread t;
    private boolean isRestarted;
    private boolean isInterrupted;

    public MyThread() {
           t = new Thread(this);
           isInterrupted = false;
           isRestarted = false;
           t.start();
    }

    public void run() {
       //Do somework
       while(true) {
         if(isInterrupted) break;
         if(isRestarted) run();
       }
    }

    public void restart() { isRestarted = true; }
    public void interupt() { isInterrupted = true; }
 }

Now when the thread isn't interupted it will wait to be restarted. When you interupt it it can't be restarted any more .

Zhivko Draganov
  • 1,213
  • 2
  • 10
  • 16