Java中定时运行程序

ExecutorService pool = Executors.newSingleThreadExecutor();
		pool.execute(new Runnable() {
			public void run() {
				for(int i=0;i<1000;i++) {
					try {
						Thread.sleep((new Random().nextInt(10) + 1) * 1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
			}
		});
		
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
		timer.scheduleAtFixedRate(new Runnable() {
			public void run() {
				System.out.println("我在运行");
				}
			}

		}, 1, 1, TimeUnit.SECONDS);

你可能感兴趣的:(Java中定时运行程序)