java – 隨時從另一個執行緒取消檔案下載
我正在使用HttpURLConnection下載檔案.我可以從另一個執行緒取消下載嗎?如果沒有,應該使用什麼檔案下載方法?
而不是HttpUrlConnection.這是一個更好的圖書館在許多方面.
然後,您可以使用:
final SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); final HttpClient client = new HttpClient(connectionManager); // ... and when it's time to close the connection from another thread connectionManager.shutdown();
如果您需要跨多個執行緒關閉多個連線,則可以重複使用一個HttpClient例項,並以類似的方式將它們全部關閉:
static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); // HttpClient instance that can be shared across threads and create multiple connections static HttpClient client = new HttpClient(connectionManager); // ... and when it's time to close connections connectionManager.shutdown();
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/4442424/cancel-file-download-from-another-thread-at-any-time