我使用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