简体   繁体   中英

Convert several Java methods 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, ie 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, ie 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(); }
}

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.

使它们符合Callable接口并将它们提供给合适的Executor(在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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM