定时器Timer如何强制停止正在执行的线程?

2020-01-14 09:45发布

timer = new Timer();
TimerTask timerTask=new TimerTask() {
public void run() {
if (flag == 0) {
timer.cancel();
}

                ltips.setText("定时任务执行中..");
                String originFile=originfile.getText();
                String checkAddress=checkaddress.getText();
                String preservAddress=preservaddress.getText();
                CscsTool cst=new  CscsTool();
                String result=cst.readAndsticky(originFile, checkAddress, 
                                    preservAddress);

                ltips.setText(result);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ltips.setText("等待执行任务中..");
            }
        };
        timer.scheduleAtFixedRate(timerTask, time, PERIOD_DAY);

标签:
1条回答
趁早两清
2楼-- · 2020-01-14 10:10

写个flag变量,定时器Timer,每次方法执行之前,判断下这个flag,

查看更多
登录 后发表回答