2016-12-09 1 views
-3

토글 버튼을 누를 때 스레드이 시작되었습니다. 이제 해당 토글 버튼을 다시 누르면 스레드가 멈추고 싶습니다. 그러나 Thread.stop() API는 더 이상 사용되지 않습니다. 그래서 UnsupportedOperationException이 발생합니다. 대신 TimerTask을 사용하는 방법을 모르겠다. 여기 내 샘플 코드는 다음과 같습니다.Thread.stop()은 더 이상 사용되지 않습니다. 대신 TimerTask를 사용하는 방법은?

//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(); 
+0

https://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html – Eric

+0

가능한 [TimerTask를 사용하여 스레드를 어떻게 실행합니까?] (http : // 10029831/how-do-you-use-a-timertask-to-run-a-thread) –

답변

2

중간 실행 중에는 단순히 스레드를 중지 할 수 없습니다. 중간 실행시 스레드를 중지하려면 Thread.interrupt() 메서드를 호출하면됩니다.

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; 
    } 
} 
0

이 작업 샘플을 시도해 볼 수 있습니다.

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; 
      } 
     }; 
    } 
} 
0

플래그를 생성하고 정지 플래그에 따라

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; } 
} 

시작을 지금

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(); 
0

당신은 당신의 자신에 의해 실행 가능한 멈출 수 없다거나 방해 할 수있는 스레드를 중지 할 수 없습니다 중지 귀하의 스레드를 호출 Thread.interrupt(). 일부 리소스를 해제해야하는 경우 을 Runnable이 아닌 Thread으로 확장하고 interrupt()을 재정의하십시오. 당신이 당신의 스레드 호출 audioDispatcherThread.interrupt()을 중지해야하는 경우

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 
    } 
} 

지금

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

하여 스레드를 시작;

관련 문제