开发者

Convert several Java methods to run as non-blocking threads?

开发者 https://www.devze.com 2023-01-16 06:56 出处:网络
Is it possible to convert a number of methods (defined in an interface and implemented in a class) to run as non-blocking threads?

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 = bi开发者_StackOverflow社区zClass; }

    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(); }
}


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.


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


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();
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消