2016-08-01 3 views
3

私のクラスでは、これらの機能があります:https://codeshare.io/kvze0
スレッドをシャットダウンしようとしても、スレッドがまだアクティブなのはなぜですか?

public void startFtp(String address, int port, String username, String password, String filepath, String file) 
{ 
    //...parsing parameter to class variables 
    try { 
     while (download) { 
      downloadAndBroadcast(); 
     } 
    } catch (Exception e){ 
     Log.d(TAG, "startFtp disconnected or fails"); 
     e.printStackTrace(); 
    } 
} 

private void downloadAndBroadcast() { 
    beginTime = System.currentTimeMillis(); 
    try { 
     URL url = new URL("http://" + serverAddress + "/" + serverFile); 
     URLConnection urlConnection = url.openConnection(); 
     urlConnection.connect(); 
     inputStream = new BufferedInputStream(url.openStream()); 
     long difference; 
     byte data[] = new byte[4094]; 
     int count; 
     while ((count = inputStream.read(data)) != -1 && download) { 
      downloadCount += count; 
      long stoptime = System.currentTimeMillis(); 
      difference = stoptime - beginTime; 
      if (difference > 1000 && download) { 
       currentSpeed = downloadCount/(difference/1000L); 
       averageSpeed = (averageSpeed + currentSpeed)/2; 
       broadcastSpeed(); 
       downloadCount = 0; 
       beginTime = stoptime; 
      } 
     } 
     clearInputStream(); 
    } catch (Exception e) { 
     Log.d(TAG, "FAIL"); 
     e.printStackTrace(); 
    } finally { 
     clearInputStream(); 
     Log.d(TAG, "downloadAndBroadcast: finally"); 
    } 
} 


private void broadcastSpeed() { 
    Log.d(TAG, "broadcastSpeed: "); 
    toMainActivityIntent = new Intent(Constants.BROADCAST_SPEED) 
      .addCategory(Intent.CATEGORY_DEFAULT) 
      .putExtra("speed", averageSpeed) 
      .putExtra("thread", String.valueOf(Thread.currentThread().getId())); 

    downloadService.sendBroadcast(toMainActivityIntent); //send to main activity, in main activity, there is a listener that analyzes Broadcast and Intent 
} 
private void clearInputStream() { 
    if (inputStream != null) { 
     try { 
      inputStream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public void stopDownload() { 
    Log.d(TAG, "stopDownload: "); 
    download = false; 
} 

私はスレッドを停止するstopDownload()を呼び出します。その後、私はスレッドのステータスを確認するために私のThreadPoolExecutorをログ:私のスレッドがアクティブになり

[Shutting down, pool size = 4, active threads = 4, queued tasks = 0, completed tasks = 0] 

何?前もって感謝します。

int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors(); 

    executor = new ThreadPoolExecutor(
      NUMBER_OF_CORES * 2, 
      NUMBER_OF_CORES * 2, 
      60L, 
      TimeUnit.SECONDS, 
      new LinkedBlockingQueue<Runnable>() 
    ); 
    executor.execute(new Runnable() { 
     public void run() { 
      FTPDownloader ftpDownloader = new FTPDownloader(downloadService); 
      ftpDownloader.startFtp(server.getServer(), server.getPort(), server.getUser(), server.getPass(), server.getPath(), server.getFiledl()); 
      if (Thread.interrupted()) { 
       Log.d(TAG, "run: "); 
       ftpDownloader.stopDownload(); 
      } 
     } 
    }); 

私がダウンして私のスレッドをシャットダウンするexecutor.shutdownNow();を呼び出します:

private class MainActivityReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      executor.shutdownNow();  
    } 
} 
+0

前にこれを追加します。しかし、とにかく - 私は 'shutdownNow()'への呼び出しを見ることができませんどのように 'stopDownload'を呼び出すと思いますか? – Fildor

+0

@FildorのshutDownNow()呼び出しは別の関数で呼び出されます。 –

+0

私は答えがあると思っていましたが、間違っていることに気付きました...ダウンロードフラグが実際にfalseに設定されていることを確認できますか?私はそう信じていない。 – Fildor

答えて

1

は、次の手順を試してください:

は、これらの行を取り除く:

私はスレッドを開始する方法

if (Thread.interrupted()) { 
      Log.d(TAG, "run: "); 
      ftpDownloader.stopDownload(); 
     } 

downloadAndBroadcastでは、私はあなたがAsyncTaskと裕福されているだろうと思います} catch (Exception e) {

} catch (InterruptedException ie) { 
    // catching IE will reset interrupted status. So just clear the flag. 
    download = false; 
} 
関連する問題