简体   繁体   English

Java线程可运行/可调用

[英]Java Threading Runnable / Callable

My current project involves sending network traffic to multiple different hosts via various TCP protocols. 我当前的项目涉及通过各种TCP协议将网络流量发送到多个不同的主机。 I am planning on making it a "plugin" type design where plugins for the different protocols can be written by implementing my interface and added at runtime. 我正计划使其成为“插件”类型的设计,其中可以通过实现我的界面来编写用于不同协议的插件,并在运行时添加它们。

Below is my initial design, I would like some pointers from more experienced developers specifically on if I am threading the correct way, and also how to "talk back" to my Swing GUI. 下面是我的初始设计,我想从更有经验的开发人员那里获得一些建议,特别是关于我是否以正确的方式进行线程化,以及如何“回聊”我的Swing GUI。

My Main class is my GUI, it extends JFrame and was created using Netbeans. 我的Main类是我的GUI,它扩展了JFrame并使用Netbeans创建。 On a button click I want to load the module (in this instance SSH - les not worry about the loading of the modules I have that done). 在按钮上单击我要加载模块(在这种情况下为SSH-不必担心我完成的模块的加载)。

The code for the button click is below: 单击按钮的代码如下:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    Executor executor = Executors.newCachedThreadPool();    
    PluginWorker worker = new PluginWorker();
    executor.execute(worker);    
}

So firstly, is there need for this Executor to call my worker? 因此,首先,该执行人是否需要打电话给我的工人? Can I use a SwingWorker here to take the execution away from my Swing GUI? 我可以在这里使用SwingWorker来使执行脱离Swing GUI吗?

The following code is my PluginWorker class, it is a class designed to launch the network probing threads, im not sure there is need for it? 以下代码是我的PluginWorker类,它是旨在启动网络探测线程的类,我不确定是否需要它? A SwingWorker may be suitable for this? SwingWorker可能适合于此吗? Also the arraylist of tasks could easily be up to 10,000 in size, so thats why I have chosen to use the invokeAll function? 任务的数组列表也可以轻松地达到10,000个大小,这就是为什么我选择使用invokeAll函数? What is best practice for that? 最佳做法是什么?

public class PluginWorker implements Runnable {
    private final ExecutorService es = Executors.newFixedThreadPool(5);  
    private final Collection<GenericPlugin> tasks = new ArrayList<>();


    @Override
    public void run() {

        SshPlugin ssh = new SshPlugin();
        tasks.add(ssh);
        //tasks could easily be up to 10,000 in size, so thats why I have chosen to use the invokeAll function?

        try {            
            es.invokeAll(tasks);
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        es.shutdown();
    }    
}

Finally, my SshPlugin which I intend can be written by anybody, extends "GenericPlugin" which implements my "Plugin" interface (so I can enforce functions like getAuthor, getDescription, etc) and also implements "Callable". 最后,我想要的SshPlugin可以由任何人编写,扩展了“ GenericPlugin”,它实现了我的“ Plugin”接口(因此我可以强制执行诸如getAuthor,getDescription等的功能),并且还实现“ Callable”。 This was needed so I could run the invokeAll from a arraylist. 这是必需的,因此我可以从arraylist运行invokeAll。 Now my concern is that my plugin is over-engineered, do I really need to extend from GenericPlugin, would it be better for my SSHPlugin to directly implements "plugin" and "callable" itself? 现在,我担心的是我的插件设计过度,是否真的需要从GenericPlugin扩展,对于我的SSHPlugin来说,直接实现“插件”和“可调用”本身会更好吗?

The short answer of your Question is that : Whenever you are interacting with GUI and some background task is to be done with continuous updates in GUI then you should use javax.swing.SwingWorker . 您的问题的简短答案是:每当您与GUI交互并且要通过GUI中的连续更新来完成某些后台任务时,都应该使用javax.swing.SwingWorker You can get to know how to use javax.swing.SwingWorker efficiently and effectively by going through this official tutorial Worker Threads and SwingWorker 通过阅读此官方教程Worker Threads and SwingWorker您可以了解如何有效地使用javax.swing.SwingWorker

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

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