final Runnable refresh = new Refresh(params...);
service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(refresh, 0, 2000, TimeUnit.MILLISECONDS);
// OR
final Thread refresh = new Refresh(params...);
refresh.start(); // In the run() method there is a loop with a sleep of 2000 ms
重复一段代码的上述哪种方法更受欢迎?为什么?
答案 0 :(得分:2)
它在功能上是等同的,但前者更灵活,责任更好(SRP):任务不应该对它的运行方式和时间负责......
答案 1 :(得分:1)