线程调度小示例:newSingleThreadScheduledExecutor用法
public class ScheduledExecutor {
static class SegTF implements ThreadFactory{
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "SegmentScheduledExecutorThread");
t.setDaemon(true);
return t;
}
}
final public static ScheduledExecutorService ScheduledService = Executors.newSingleThreadScheduledExecutor(new SegTF());
public static void submit(Runnable cmd, long periodMilliSenconds){
ScheduledService.scheduleAtFixedRate(cmd, 10l, periodMilliSenconds, TimeUnit.MILLISECONDS);
}
public static void main(String[] args) throws InterruptedException {
ScheduledExecutor.submit(new Runnable(){
public void run() {
System.out.println("do something");
}
}, 1000);
ScheduledExecutor.submit(new Runnable(){
public void run() {
System.out.println("do another thing");
}
}, 1000);
TimeUnit.SECONDS.sleep(10);
}
}