将多个 Java 方法转换为非阻塞线程运行?
是否可以将多个方法(在接口中定义并在类中实现)转换为作为非阻塞线程运行?
当然,我可以将每个单独的方法包装在线程类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据上面的问题和评论,您希望调用一个方法来导致任务的异步执行。最好的方法是通过 Runnable 和 ExecutorService 的实例。
这样想,为了异步运行,您需要向另一个线程发送某种消息以指示它应该执行工作。 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.
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(执行者)
Make them conform to the Callable interface and provide them to a suitable Executor (plenty to choose from in Executors)
而不是继承自
java.lang.Thread
并覆盖run()
方法创建java.lang.Runnable
按所需顺序调用方法。Runnable
实现可以引用提供这些方法的类,也可以是类本身的一部分。Rather than inherit from
java.lang.Thread
and override therun()
method it would be cleaner to create an implementation ofjava.lang.Runnable
that called the methods in the desired order. TheRunnable
implementation could have a reference to your class providing these methods or could be part of the class itself.