java - scheduleWithFixedDelay with concurrent overlapping tasks -
i looking variant of scheduledexecutor allows task run @ specific interval without waiting previous task complete.
given code below, there 12 different lines of output @ intervals of 5000ms.
given execution interval of 50ms , thread pool size of 10, looking solution has 10 output lines in first 550ms, followed pause until threads freed , can reused.
scheduledexecutorservice pollingexecutorservice = executors.newscheduledthreadpool(10); final runnable pollingtask = new runnable() { public void run() { system.out.println("running poller " + datetime.now().tostring()); try { thread.sleep(5000); } catch (interruptedexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } system.out.println("5000 passed"); } }; scheduledfuture<?> pollinghandler = pollingexecutorservice.schedulewithfixeddelay(pollingtask, 0, 50, timeunit.milliseconds); //wait secondsidlebeforeshutdown seconds try { thread.sleep(1000*60); } catch (interruptedexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. }
try this
executorservice ex = executors.newfixedthreadpool(10); (;;) { list<future<?>> list = new arraylist<>(); (int = 0; < 10; i++) { future<?> f = ex.submit(new runnable() { @override public void run() { system.out.println("running poller " + new date()); try { thread.sleep(5000); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("5000 passed"); } }); list.add(f); } (future<?> f : list) { f.get(); } }
Comments
Post a Comment