views:

83

answers:

3

Is it possible to convert a number of methods (defined in an interface and implemented in a class) to run as non-blocking threads?

Certainly, i can wrap each single method in the run() method of a thread class. But perhaps there exists a more sophisticated way to warp several different methods in one step, i.e. by a single thread class wrapper?

According to the example of 'Adamski' below, i don't want to create a new Runnable class for every method of the interface, i.e. i would like to avoid the following:

public interface MyBusinessClass 
{
    void a();
    void b();
}


public class MyRunnable_a  implements Runnable 
{
    private final MyBusinessClass bizClass;
    public MyRunnable_a(MyBusinessClass bizClass) { this.bizClass = bizClass; }

    public void run() { bizClass.a(); }
}


public class MyRunnable_b  implements Runnable 
{
    private final MyBusinessClass bizClass;
    public MyRunnable_b(MyBusinessClass bizClass) { this.bizClass = bizClass; }

    public void run() { bizClass.b(); }
}
+1  A: 

Rather than inherit from java.lang.Thread and override the run() method it would be cleaner to create an implementation of java.lang.Runnable that called the methods in the desired order. The Runnable implementation could have a reference to your class providing these methods or could be part of the class itself.

/**
 * Business class that defines the methods to be run in a dedicated thread.
 * Classes implementing this interface are responsible for thread safety.
 */
public interface MyBusinessClass {
  void a();

  void b();

  void c();
}

/**
 * Runnable implementation that calls the methods defined on MyBusinessClass.
 */
public class MyRunnable implements Runnable {
  private final MyBusinessClass bizClass;

  public MyRunnable(MyBusinessClass bizClass) {
    this.bizClass = bizClass;
  }

  public void run() {
    bizClass.a();
    bizClass.b();
    bizClass.c();
  }
}
Adamski
OK, but i would like to give the user the ability to invoke a single method of the interface, which than runs in an asynchronous fashion...
rmv
I'm not sure I follow - You want to give the user the ability to do what? To determine which methods to run and in what order? Is this through a GUI? It might be better if you expand your original question.
Adamski
+3  A: 

Make them conform to the Callable interface and provide them to a suitable Executor (plenty to choose from in Executors)

Thorbjørn Ravn Andersen
+1  A: 

Based on your question and comment above, you would like the invocation of a method to result in the asynchronous performance of a task. The best way to do this is via instances of Runnable and an ExecutorService.

public class MyBusinessClass {
  ExecutorService myExecutor = Executors.newCachedThreadPool(); //or whatever

  void a(){
    myExecutor.execute(new Runnable() {
        public void run() {
          doA();
        }
    });
  }    

  void b(){
    myExecutor.execute(new Runnable() {
        public void run() {
          doB();
        }
    });
  }    
}

Think of it this way, in order to run asynchronously, you need to send some kind of message to another Thread to indicate it should do work. The Executor framework in the java.util.concurrent package is the standardized way of forming those messages. They are formed in such a way that the "run" method on the Runnable instance indicates what actions should be taken.

Tim Bender
Thanks, that's what i was looking for!
rmv