In a classes are some methods that are run synchronously. I would like them to run asynchronously, the first idea was to wrap it, and use switch enum to decide which function should be called. But for every method called, I needed a new method in the wrapper class and a new enum. It looks like this:
public class QueuedShow implements InterfaceShowAlert, Runnable {
private Class Action {
String param1;
public static enum ActionEnum{
NEW, CHECK, UPDATE, DELETE, DISPLAY;
}
public ActionEnum todo;
public Action(ActionEnum todo, Object param){
this.todo=todo;
this.param1=param;
}
}
private BlockingQueue<Action> actionList;
public void run(){
while(true){
Action action=actionList.take(); //THIS waits until queue isn't empty
switch(action.todo){
case NEW: //Call original function
....
}
}
}
public void new(String param){
actionList.add(new Action(NEW, param));
}
}
Then I learned about reflection and I got a new idea. That is to invoke methods using strings instead of direct method calls. The wrapper class reads and parses the strings, and gets the class and containing method using reflection. It puts the parameters and Method object into a class, which is put into a queue. Now the class uses Method.invoke(params) instead of using a enum to switch. But the problem of this is that compiler time type checking is lost.
Of course, all this works for only methods that are void, but of course we could use the Future class to return values too.
Now is there any framework that already has implemented turning synchronous calls to asynchronous ones, or do you know of any other method that does this.