views:

261

answers:

3

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.

+3  A: 

The normal way to handle this is to use the java 5 executors framework, like shown here. This'll allow you to use the same pattern for both synchronous and asynchronous implementations, and you can just choose which ExecutorService to use.

krosenvold
Yeah, but that means you have to make a new wrapper class for EVERY method, implementing a run() method.
TiansHUo
Yes. Most people just use anonymous inner classes.
krosenvold
+4  A: 

This is the recommended way of doing it:

abstract public class Action<T> implements Runnable {
  private final T param;

  public Action(T param) {
    this.param = param;
  }

  @Override
  public final void run() {
    work(param);
  }

  abstract protected void work(T param);
}

with:

ExecutorService exec = Executors.newSingleThreadExecutor();
while (/* more actions need to be done */) {
  exec.submit(action);
}
exec.shutdown();
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
cletus
But that is the same thing as krosenvoid, it means that a wrapper class is needed for every method, implementing a work(param) method now.
TiansHUo
@Tians yes but it's better than using a switch statement of if-blocks to call static functions. It can easily be extended. Also, it should be noted that you could put the method directly on the enum itself.
cletus
@cletus, What I would really like is a method, that is implement and forget. Instead, this means that I will have to write a wrapper for every method I want to change to asynchronous. I took a look a Groovy, it seems that it may work.
TiansHUo
@Tians just create anonymous inner classes. That's just how Java works. Swing dev is full of them. The only alternative is hard coding everything. Are anonymous inner classes really that odious?
cletus
@cletus +1 Fully agree with you. I wrote a complement to your answer to try to convince the OP
ewernli
@cletus I like your anonymous inner class way, so I'm marking yours as the answer. I'll try it some day
TiansHUo
+1  A: 

But that is the same thing as krosenvoid, it means that a wrapper class is needed for every method, implementing a work(param) method now.

Either you go for something that is fully dynamic and reflective, where you pass String for method name and Object for parameter. Because Java is statically typed (unlike dynamic language like Groovy) that will be horrible and probably full of casting, but you can do it.

You current solution lists the asynchronous method with enums. So it's not fully dynamic and extensible neither without changing the code. That is, you will need to add a new enum for each new method.

public static enum ActionEnum{
          NEW, CHECK, UPDATE, DELETE, DISPLAY;
}

If it's ok for you, it should also be ok to use Action or Callable, as suggested in the other answers. For each new method you will need to write a new Action or Callable so that the method invocation is statically defined.

 protected void work(String param)
 {
      myObject.theMethodToDispatch( param );
 }

I agree that it's a bit more boilerplate code, but that's still fairly small. The big win is that then your code is not reflective (and there should be no casting) and you have the benefit of statically typed languages: you can refactor method names, the compiler warns you if type parameter mismatch, etc.

ewernli
@erwernli thanks for the explanation
TiansHUo