小编nai*_*dat的帖子

如何使用ScheduledExecutorService返回值?

我使用ScheduledExecutorService,我希望它每隔10秒进行一次计算一分钟,然后在那一分钟后给我返回新的值.我该怎么做?

示例:所以它收到5它增加+1六次然后它应该在一分钟后返回值11.

到目前为止,我没有工作的是:

package com.example.TaxiCabs;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.*;


public class WorkingWithTimeActivity {
public int myNr;
public WorkingWithTimeActivity(int nr){
    myNr = nr;
}
private final ScheduledExecutorService scheduler =
        Executors.newScheduledThreadPool(1);

public int doMathForAMinute() {
    final Runnable math = new Runnable() {
        public void run() {
            myNr++;
        }
    };
    final ScheduledFuture<?> mathHandle =
            scheduler.scheduleAtFixedRate(math, 10, 10, SECONDS);
    scheduler.schedule(
            new Runnable() {
                public void run() {
                    mathHandle.cancel(true);
                }
            }, 60, SECONDS);
    return myNr;
}
Run Code Online (Sandbox Code Playgroud)

}

并在我的主要活动中,我希望它在1分钟后将我的txtview文本更改为11;

WorkingWithTimeActivity test …
Run Code Online (Sandbox Code Playgroud)

java time android timer scheduledexecutorservice

3
推荐指数
1
解决办法
8952
查看次数

标签 统计

android ×1

java ×1

scheduledexecutorservice ×1

time ×1

timer ×1