我想首先执行一个方法,然后再执行另一个方法,这取决于第一个方法的结果。为了保持更新,我使用了ScheduledExecutorService:
Runnable alternatingExecution = new Runnable() {
@Override
public void run() {
new NetworkCall().execute(""); //this should be executed in first place
methodTwo(); //this should be executed after
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
executor.scheduleAtFixedRate(alternatingExecution, 0, 1, TimeUnit.SECONDS);
}
我尝试在其间设置sleep();
,但遗憾的是它不起作用。据我所知Runnable
也只定义了一种方法。那么我怎么能解决这个问题,不断在两种方法之间交替?