线程池ScheduledThreadPool
定时线程池
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3); Runnable runnable = new Runnable() { @Override public void run() { Log.d("Thread", "This task is delayed to execute"); } }; scheduledThreadPool.schedule(runnable,10, TimeUnit.SECONDS);//延迟启动任务 //延迟5s后启动,每1s执行一次 scheduledThreadPool.scheduleAtFixedRate(runnable,5,1,TimeUnit.SECONDS); //启动后第一次延迟5s执行,后面延迟1s执行 scheduledThreadPool.scheduleWithFixedDelay(runnable,5,1,TimeUnit.SECONDS); } }