java - why ExecutorService is not getting frequently executed -


there method need update every specific time , testing java executorservice , method not getting updated , please tell me why ?

these classes

futuretask.java

package com;  import java.lang.reflect.method; import java.util.concurrent.*;  public class futuretask {     private static executorservice executor = executors.newcachedthreadpool();     private static futuretask _instance = new futuretask();      public static futuretask getinstance() {         return _instance;     }      private static int timoutsec = 15;      public object submitetask(final object obj, final method method,             final object[] params) throws exception {         return submitetask(obj, method, params, -1);     }      public object submitetask(final object obj, final method method,             final object[] params, int timeoutseconds) throws exception {         if (null != obj && method != null) {             callable<object> task = new callable<object>() {                 public object call() {                     try {                         method.setaccessible(true);                         object resultobj = method.invoke(obj, params);                         return resultobj;                     } catch (exception e) {                     }                     return null;                 }             };             future<object> future = executor.submit(task);             try {                 object result = null;                 if (timeoutseconds < 0) {                     result = future.get(timoutsec, timeunit.seconds);                 } else {                     result = future.get(timeoutseconds, timeunit.seconds);                 }                 return result;             } catch (timeoutexception e) {             } catch (exception e) {             } {                 future.cancel(true);             }         }         return null;     }      public static void main(string args[]) {         try {             futuretask.getinstance().submitetask(                     new testingfuturetaskutil(),                     testingfuturetaskutil.class.getdeclaredmethod(                             "updatemethodcalled",                             new class<?>[] { string.class }),                     new object[] { "ubsc!oc1010" }, 1);         } catch (exception e) {             e.printstacktrace();         }     } } 

testingfuturetaskutil.java

package com;  public class testingfuturetaskutil {      public void updatemethodcalled(string symbol) {          system.out.println("updatemethodcalled" + symbol);      }  } 

thanks in advance .

you using normal executorservice. doesn't allow schedule tasks. need use scheduledexecutorservice.

you need change following:

private static scheduledexecutorservice executor = executors.newscheduledthreadpool(poolsize); 

and:

future<object> future = executor.scheduleatfixedrate(task, timeoutseconds, timeoutseconds, timeunit.seconds); 

now task executed every "timeoutseconds" seconds. afterwards can return scheduledfuture , can updated values it.

maybe because of example create callable outside , hand futuretask. don't need reflection. way doing asynchronous call wrong because calling thread waits computation finish. therefore, don't gain benefits running method in other thread. maybe need rethink whole design of doing.


Comments