2016-08-01 31 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

添加此我想你会一直用的AsyncTask更好。但无论如何 - 我看不到'shutdownNow()'的调用,以及它应该如何调用'stopDownload'? – Fildor

+0

@Fildor调用shutDownNow()在另一个函数中被调用。 –

+0

我以为我有答案,但我意识到这是错误的...你可以验证下载标志实际上设置为false?我不这么认为。 – Fildor

回答

1

尝试以下步骤:

摆脱这些行:

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

downloadAndBroadcast} catch (Exception e) {

} catch (InterruptedException ie) { 
    // catching IE will reset interrupted status. So just clear the flag. 
    download = false; 
} 
相关问题