2014-10-10 131 views
0

我正在尋找一種方法來停止/殺死一個線程,我看到Thread.stop()已被棄用。所以我開始尋找另一種解決方案,看到多個職位表明這一點:尋找一種方法來停止/殺死一個線程

thread.interrupt(); 
thread = null; 

但是,就不會停止我的主題,我的主題是這樣的:

public static Thread loadingSpinnerTimer (final Context context, final ImageView imageView){ 
    final Handler mHandler = new Handler(); 
    thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      while (!isPaused) { 
       try { 
        Thread.sleep(100); 
        mHandler.post(new Runnable() { 
         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          // Write your code here to update the UI. 
          Log.e("ThreadMessage","HasRun"); 
          if (ticker == 12){ 
           ticker = 0; 
          } 
          Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.spinner_160x160); 
          Bitmap img = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.ARGB_8888); //ARGB_8888 transparrent? 
          Canvas canvas = new Canvas(img); 
          int offset = myBitmap.getWidth()*ticker; 
          Rect srcrectangle = new Rect(0, offset, myBitmap.getWidth(), myBitmap.getWidth()+offset); 
          Rect dstrectangle = new Rect(0, 0, myBitmap.getWidth(), myBitmap.getWidth()); 
          canvas.drawBitmap(myBitmap,srcrectangle,dstrectangle,null); 
          img.setHeight(myBitmap.getWidth()); 
          imageView.setImageDrawable(new BitmapDrawable(context.getResources(), img)); 
          ticker++; 
         } 
        }); 
       } catch (Exception e) { 
        // TODO: handle exception 
       } 
      } 
     } 
    }); 
    thread.start(); 
    return thread; 
} 

任何人都得到一個解決方案,我可以用來停止/殺死我的線程?任何幫助深表感謝。

+0

檢查[復位標誌oracle docs](http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html) – TheLostMind 2014-10-10 15:08:43

+0

看到這個[link](http://stackoverflow.com/a/20528033/4101719) – coderhyme 2014-10-10 15:22:00

回答

1

您有一個while循環並檢查isPaused,然後在循環結束處捕獲常規Exception。即使InterruptedException被引發,它也會被捕獲,然後再次運行while循環。

當您捕獲InterruptedException異常以便while循環停止運行時,您需要將isPaused更改爲true。

+0

是的,謝謝!我是怎麼忽略的:) – user2408952 2014-10-10 18:41:52

0

由於您定期執行任務,所以最好的解決方案是使用ScheduledExecutorService

然後計劃任務可以優雅停止呼籲cancel()ScheduledFuture<?>

final Runnable handlerRunnable = new Runnable() { 
    @Override 
    public void run() { 
     // do same work as before 
    } 
}; 
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); 
final Handler mHandler = new Handler(); 
ScheduledFuture<?> sf = ses.scheduleWithFixedDelay(new Runnable() { 
    @Override 
    public void run() { 
     mHandler.post(handlerRunnable); 
    } 
}, 0, 100, TimeUnit.MILLISECONDS); // execute every 100ms 

// stop 
sf.cancel(); 
0

如果使用thread.interrupt();你應該偶爾會檢查你的Thread使用像所中斷:

if(Thread.currentThread().isInterrupted()) { 
    return; 
} 

注意檢查這個標誌就復位;所以,如果你曾經檢查該標誌您應該:

終止您的線程

  1. 列表項
  2. 拋出一個InterruptedException
  3. 通過調用Thread.currentThread().interrupt();