import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
public class TestSchedua {
public static void main(String[] args) { ScheduledExecutorService reSendTablesScheduled = Executors.newScheduledThreadPool(3, new CustomizableThreadFactory("gaga-")); reSendTablesScheduled.scheduleWithFixedDelay(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("11111111:" + Thread.currentThread().getName()); } }, 3, 3, TimeUnit.SECONDS); reSendTablesScheduled.scheduleWithFixedDelay(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("22222222:" + Thread.currentThread().getName()); } }, 3, 3, TimeUnit.SECONDS); System.out.println("end"); }
}
输出: end 11111111:gaga-1 22222222:gaga-2
11111111:gaga-1 22222222:gaga-3
11111111:gaga-2 22222222:gaga-1
11111111:gaga-3 22222222:gaga-2 异步执行,执行完一次,delay后,才会执行下一次。 本次卡住了,就影响下一次执行了。 线程池,就是用哪些线程跑~
如果线程池大小是1,那么两个Scheduled就要排队执行
|