将多个 Java 方法转换为非阻塞线程运行?

发布于 2024-09-19 03:27:56 字数 720 浏览 7 评论 0原文

是否可以将多个方法(在接口中定义并在类中实现)转换为作为非阻塞线程运行?

当然,我可以将每个单独的方法包装在线程类的 run() 方法中。但也许存在一种更复杂的方法来在一个步骤中扭曲多个不同的方法,即通过单线程类包装器?

根据下面的“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(); }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-09-26 03:27:56

根据上面的问题和评论,您希望调用一个方法来导致任务的异步执行。最好的方法是通过 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();
        }
    });
  }    
}

这样想,为了异步运行,您需要向另一个线程发送某种消息以指示它应该执行工作。 java.util.concurrent 包中的 Executor 框架是形成这些消息的标准化方法。它们的形成方式使得 Runnable 实例上的“run”方法指示应采取哪些操作。

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.

独留℉清风醉 2024-09-26 03:27:56

使它们符合 Callable 接口,并将它们提供给合适的 Executor(执行者)

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

话少心凉 2024-09-26 03:27:56

而不是继承自 java.lang.Thread 并覆盖 run() 方法创建 java.lang.Runnable 按所需顺序调用方法。 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();
  }
}

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();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文