java抓取当前任务,java – ThreadPoolExecutor:获取正在执行的特定Runnable

我正在使用ThreadPoolExecutor在后台执行多个长时间运行的任务,ThreadPoolExecutor的池大小为4,因此当添加超过4个任务时,它们被推送到队列,当4个任务中的一个完成时,一个任务从队列中执行以执行.

我想知道有没有办法访问当前正在执行但不在队列中的Runnable对象,即前4个任务.

目的:我想在任何给定点获取任务的当前状态,在mThreadPoolExecutor.getQueue()的帮助下我正在访问排队并准备执行的任务,请建议我访问当前正在执行的任务这样我就可以在需要时附加和删除它上面的监听器/处理程序.

我的Runnable类:

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 itr = mThreadPoolExecutor.getQueue().iterator();

while (itr.hasNext()) {

VideoFileUploadRunner runner = (VideoFileUploadRunner) itr.next();

if (runner.getPK() == fileSync.get_idPrimaryKey()) {

return runner;

}

}

return null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值