- public class Test {
- public static void main(String[] args) {
- //第一种
- /*
- ScheduledExecutorService se = Executors.newScheduledThreadPool(1);
- se.scheduleAtFixedRate(new Runnable(){
- public void run() {
- System.out.println(1);
- }
- }, 1, 1, TimeUnit.SECONDS);
- */
- //第二种
- Executors.newSingleThreadExecutor().execute(new Runnable(){
- public void run() {
- while(true){
- try {
- Thread.sleep(1000);
- System.out.println(2);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- });
- }
- }
复制代码 这两种方法有什么区别? |