简体   繁体   English

转换几个Java方法作为非阻塞线程运行?

[英]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. 当然,我可以在线程类的run()方法中包装每个单独的方法。 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: 根据下面'Adamski'的例子,我不想为接口的每个方法创建一个新的Runnable类,即我想避免以下情况:

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. 执行此操作的最佳方法是通过Runnable和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. java.util.concurrent包中的Executor框架是形成这些消息的标准化方法。 They are formed in such a way that the "run" method on the Runnable instance indicates what actions should be taken. 它们的形成方式是Runnable实例上的“run”方法指示应采取的操作。

使它们符合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. 而不是从java.lang.Thread并覆盖run()方法,创建一个以所需顺序调用方法的java.lang.Runnable实现会更加清晰。 The Runnable implementation could have a reference to your class providing these methods or could be part of the class itself. Runnable实现可以引用提供这些方法的类,也可以是类本身的一部分。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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