java - How is it possible that an interface is being instantiated in this sample code? -
this question has answer here:
a manual i'm reading includes example, scheduledexecutorservice
being created. however, api shows scheduledexecutorservice
interface, not class. how possible being instantiated?
here's sample code shown:
import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.executors; import java.util.concurrent.scheduledfuture; import static java.util.concurrent.timeunit.*; class beepercontrol { private final scheduledexecutorservice scheduler = executors.newscheduledthreadpool(1); public void beepforaminute() { final runnable beeper = new runnable() { public void run() { system.out.println("beep"); } }; final scheduledfuture<?> future = scheduler.scheduleatfixedrate(beeper, 250, 250, milliseconds); scheduler.schedule( new runnable() { public void run(){ future.cancel(true); } }, 3, seconds ); while (!future.isdone()) { try { thread.sleep(10); } catch(interruptedexception e) {} } scheduler.shutdown(); } public static void main(string[] args) { beepercontrol bc = new beepercontrol(); bc.beepforaminute(); } }
executors.newsceduledthreadpool(int) factory method returns class implements scheduledexecutorservice
interface.
edit:
as per grepcode:
public static scheduledexecutorservice newscheduledthreadpool( int corepoolsize, threadfactory threadfactory) { return new scheduledthreadpoolexecutor(corepoolsize, threadfactory); }
so here returning new instance of scheduledthreadpoolexecutor
implements scheduledexecutorservice
interface.
Comments
Post a Comment