fixedthreadpoolでscheduledThread
shutdown methodでループを止める
public class Outer {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService s = Executors.newScheduledThreadPool(2);
s.scheduleWithFixedDelay(() -> {
System.out.print("a");
},1,1,TimeUnit.SECONDS);
Thread.sleep(500);
s.scheduleWithFixedDelay(() -> {
System.out.print("b");
},1,1,TimeUnit.SECONDS);
Thread.sleep(10000);
s.shutdown();
}
}
abababababababababa