-2

I have started a thread on press of a toggle button. Now I want to stop that thread on pressing that toggle button again. But Thread.stop() API has been deprecated. So, I am getting UnsupportedOperationException. I don't know how to use TimerTask as an alternative to it. Here is my sample code:

//AudioDispatcher implements a Runnable
public class AudioDispatcher implements Runnable

//This is a code to start a thread
AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);
Thread t1 = new Thread(dispatcher,"Audio Dispatcher");
t1.start();
Rohit Singla
  • 112
  • 2
  • 9
  • https://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html – Eric Dec 09 '16 at 06:45
  • Possible duplicate of [How do you use a TimerTask to run a thread?](http://stackoverflow.com/questions/10029831/how-do-you-use-a-timertask-to-run-a-thread) – Rohit Singla Apr 25 '17 at 08:40

4 Answers4

3

You can't simply stop a thread in mid execution. If you want to stop a thread in mid execution then you can call Thread.interrupt() method.

public class SomeBackgroundProcess implements Runnable {

Thread backgroundThread;

public void start() {
   if( backgroundThread == null ) {
      backgroundThread = new Thread( this );
      backgroundThread.start();
   }
}

public void stop() {
   if( backgroundThread != null ) {
      backgroundThread.interrupt();
   }
}

public void run() {
    try {
       Log.i("Thread starting.");
       while( !backgroundThread.interrupted() ) {
          doSomething();
       }
       Log.i("Thread stopping.");
    } catch( InterruptedException ex ) {
       // important you respond to the InterruptedException and stop processing 
       // when its thrown!  Notice this is outside the while loop.
       Log.i("Thread shutting down as it was requested to stop.");
    } finally {
       backgroundThread = null;
    }
}
Dhrumil Patel
  • 544
  • 6
  • 16
1

Create a flag and stop according to flag

 class Server implements Runnable{
      private volatile boolean exit = false;

      public void run() {

     while(!exit){ 
        System.out.println("Server is running....."); 
    } 

    System.out.println("Server is stopped...."); 
    } 

    public void stop(){ exit = true; } 
}

start and stop now

  Server myServer = new Server(); 
    Thread t1 = new Thread(myServer, "T1"); 
    t1.start(); 
//Now, let's stop our Server thread 
System.out.println(currentThread().getName() + " is stopping Server thread"); myServer.stop();
vishal jangid
  • 2,967
  • 16
  • 22
0

You can try this time task sample;

private static final int DEFAULT_DELAY_TIME = 1000;
private static final int DEFAULT_PERIOD = 1000;
private Timer mPlayTimer;
private TimeTask mPlayPlanTask;

private void startTask() {
        configTimer();
        mPlayTimer.schedule(mPlayPlanTask, DEFAULT_DELAY_TIME,
              DEFAULT_PERIOD);
    }
}

private void stopTimer() {
    if (mPlayTimer != null) {
        mPlayTimer.cancel();
        mPlayTimer = null;
    }
    if (null != mPlayPlanTask) {
        mPlayPlanTask.cancel();
        mPlayPlanTask = null;
    }
}

private void configTimer() {
    if (null == mPlayTimer) {
        mPlayTimer = new Timer();
    }
    if (null == mPlayPlanTask) {
        mPlayPlanTask = new TimerTask() {
            @Override
            public void run() {
                  // Do some work;
            }
        };
    }
}
Yat3s
  • 330
  • 1
  • 11
0

You can not stop runnable by your self or can not stop a thread you can interrupt your thread by calling Thread.interrupt(). Extend your AudioDispatcher to Thread rather than Runnable an override interrupt() if you need to release some resources.

public class AudioDispatcher extends Thread {

    @Override
    public void run() {
        // check if thread is interrupted do nothing
        if(!Thread.currentThread().isInterrupted()){

        }
    }

    @Override
    public void interrupt() {
        super.interrupt();
        // release resources if any
    }
}

Now start your thread by

AudioDispatcher audioDispatcherThread = new AudioDispatcher();
audioDispatcherThread.start();

When you need to stop your thread call audioDispatcherThread.interrupt();

Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28