我使用ThreadPoolExecutor
執行在後臺多長時間運行的任務的具體Runnable接口的ThreadPoolExecutor
池大小是4所以正在增加超過4任務時,他們都推到隊列中,並且當一個4個任務完成後,一個任務從隊列中移出執行。的ThreadPoolExecutor:獲取正在執行
我想知道有什麼方法可以訪問Runnable
當前正在執行的對象,而不是隊列,即前4個任務。
目標:我想要在任何給定點上獲得當前任務的狀態,在mThreadPoolExecutor.getQueue()
的幫助下,我正在訪問正在排隊並準備執行的任務,請告訴我如何訪問當前正在執行的任務這樣我就可以在需要時附加和移除監聽器/處理器。
我運行的類:
public class VideoFileUploadRunner implements Runnable {
private final VideoFileSync mVideoFileSync;
private final DataService dataService;
private Handler handler;
public VideoFileUploadRunner(VideoFileSync videoFileSync, DataService dataService) {
this.mVideoFileSync = videoFileSync;
this.dataService = dataService;
}
public int getPK()
{
return mVideoFileSync.get_idPrimaryKey();
}
public void setHandler(Handler handler) {
this.handler = handler;
}
@Override
public void run() {
try {
if (mVideoFileSync.get_idPrimaryKey() < 0) {
addEntryToDataBase();
}
updateStatus(VideoUploadStatus.IN_PROGRESS);
FileUploader uploader = new FileUploader();
updateStatus(uploader.uploadFile(mVideoFileSync.getVideoFile()));
} catch (Exception e) {
updateStatus(VideoUploadStatus.FAILED);
e.printStackTrace();
}
}
private void addEntryToDataBase() {
int pk = dataService.saveVideoRecordForSync(mVideoFileSync);
mVideoFileSync.set_idPrimaryKey(pk);
}
private void updateStatus(VideoUploadStatus status) {
if (handler != null) {
Message msg = new Message();
Bundle b = new Bundle();
b.putString(AppConstants.Sync_Status, status.toString());
msg.setData(b);
handler.sendMessage(msg);
}
dataService.updateUploadStatus(mVideoFileSync.get_idPrimaryKey(), status.toString());
}
}
在任務進度列表視圖持有人:
public void setData(VideoFileSync fileSync) {
tvIso.setText(fileSync.getVideoFile().getISO_LOOP_EQUP());
tvUnit.setText(fileSync.getVideoFile().getUnit());
tvName.setText(fileSync.getVideoFile().getLocalPath());
tvStatus.setText(fileSync.getCurentStatus().toString());
addHandleForUpdate(fileSync);
}
private void addHandleForUpdate(VideoFileSync fileSync) {
Handler.Callback callBack = new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if(msg.getData()!=null)
{
tvStatus.setText(msg.getData().getString(AppConstants.Sync_Status));
}
return false;
}
};
mHadler = new Handler(Looper.getMainLooper(),callBack);
VideoFileUploadRunner runner = VideoUploadManager.getInstance().getRunnerForSyncFile(fileSync);
if(runner!=null)
runner.setHandler(mHadler);
}
在VideoUploadManager
我有以下方法返回Runnable
對象,在這裏,我想幫助,使我可以返回任務正在執行。
public synchronized VideoFileUploadRunner getRunnerForSyncFile(VideoFileSync fileSync) {
Iterator<Runnable> itr = mThreadPoolExecutor.getQueue().iterator();
while (itr.hasNext()) {
VideoFileUploadRunner runner = (VideoFileUploadRunner) itr.next();
if (runner.getPK() == fileSync.get_idPrimaryKey()) {
return runner;
}
}
return null;
}
*,這樣我可以附加和以往任何時候都需要的時候就可以刪除監聽器/處理器*。你能詳細說明你的意思嗎? – CKing
我正在開發移動應用程序,其中我有一個屏幕來顯示任務的當前狀態,用戶可以關閉應用程序並返回檢查狀態,所以當用戶在屏幕上時,我想將處理程序附加到可運行對象 – DCoder
而不是runnable的外部嘗試通過執行程序找到可運行的程序並將偵聽程序附加到它上面。從runnables run方法內部綁定到外部偵聽程序,並在方法結束時解除綁定。因此,您可以發佈您的事件,在可運行的應用程序中訂閱和取消訂閱,並且只有當前活動的可運行應用程序將發佈其更新。 – Charlie