java - executorService.scheduleAtFixedRate to run task forever -
i want task run forever after interval of 1 min. accomplish wrote task in
public void poll() { scheduledexecutorservice executorservice= executors.newscheduledthreadpool(1); scheduledfuture files=executorservice.scheduleatfixedrate(new runnable() { @override public void run() { string path="/desktop/pnl/test"; list<string> filespaths= new arraylist<string>(); file folder= new file(path); file[] listoffiles= folder.listfiles(); (file file : listoffiles) { filespaths.add(file.getabsolutepath()); } if(!collectionutils.isempty(filespaths)){ try{ update(filespaths); } catch(exception e){ system.out.println(e.tostring()); } } } }, 0, 1, timeunit.minutes); files.cancel(false); //executorservice.shutdown(); }
but task executes once , not after each minute. don't understand whats wrong here.
while executing code, there nullpointerexception
caused @ for (file file : listoffiles) {
killed thread.
the following change made run continuously:
if (listoffiles != null) { (file file : listoffiles) { filespaths.add(file.getabsolutepath()); } }
moreover, files.cancel(false)
ends execution. hence, had comment line. reference of future.cancel()
Comments
Post a Comment