I am trying to execute two method where two methods will execute one by one after some interval, and I am using ExecuterService
. I have implemented some portion of the code but the full functionality I could not achieve till now, here I am posting my code
public class ExampleExecuterService {
private static final int MYTHREADS = 3000;
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
Object methodList[]={
aMethod(),bMethod()
};
for(int i=0;i<methodList.length;i++){
Object myList = methodList[i];
Runnable worker = new MyRunnable(myList);
executor.execute(worker);
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("\nFinished all threads");
}
public static class MyRunnable implements Runnable {
private Object myList=null;
MyRunnable(Object myList) {
this.myList = myList;
}
@Override
public void run() {
try{
myList.wait(2000);
}catch(Exception e){
e.printStackTrace();
}
}
}
private static Object bMethod() {
System.out.println("This is inside method a ");
return null;
}
private static Object aMethod() {
System.out.println("This is inside method b ");
return null;
}
}
I want aMethod()
and bMethod()
that should run 20 seconds after and in the end the executer
will stop. How to do that with my code. Somebody please help me.
This is not a list of your methods. This is a list of what your methods return (=null).
In Java, methods are not objects. If you want to store methods in a list or array, you have to wrap them inside objects. The usual way of doing this is by using the
Runnable
interface or something of the kind.In your case, it could look like this:
After that you can submit this array of "methods" to your executor service:
However, keep in mind that
ExecutorService
is primarily meant to execute tasks concurrently (= in parallel), while you want to execute them sequentially.In you know that you'll always need a sequential, single-thread behaviour, you should drop
ExecutorService
and simply:EDIT: Full main method