java - 在多个 JPanel 上运行多个线程并切换它们

标签 java multithreading swing

我有一个具有多线程功能的 TCP 服务器,可以从 cli 处理它。

现在我想为它编写一个 GUI,以便我可以轻松启动、监视、管理和退出该多线程服务器的特定实例。首先我想到了 JTable,这也是我的第一个问题:

这是处理 JTable 内多个套接字(而不是线程)上的多个服务器线程的好方法(甚至可能)吗?我是否能够向其发送数据以根据套接字 Activity 更新各个行?我的意思是,当每一行代表一个正在运行的服务器线程时,用户应该能够单击它,启动一个新线程(将添加到列表中),查看字段中的套接字状态(无论是什么),查看有多少个客户端已连接,甚至能够通过该表停止特定线程?

我的第二个问题是:

当我开始按照上面描述的方式实现它时,当我使用另一个接口(interface)更新 Activity JPanel 并再次切换回来时,jtable 和正在运行的线程会发生什么情况?例如,我有一个界面用于概述所有实例,另一个界面用于了解单个实例的详细信息。所有线程都会被中断吗?或者之后它们还能再次与表通信吗?或者使用cardLayout更好?

当我更改卡片布局上的卡片并且该卡片上正在运行某些内容(例如,正在运行套接字线程的 JTable)时,它们会被中断或停止吗?或者我可以再更新 JTable 中的状态数据吗?因此套接字应该能够向表中写入和更新数据。

现在是我的最后一个问题,感谢您阅读这么久。在尝试寻找上述所有问题的答案时,我尝试了一个简单的 JPanel 切换框架,每个面板上都有一个 JTextarea,该框架写入 100 行,中间有 1000 毫秒的中断。然后我想切换面板,看看该任务是否被中断。不幸的是,我什至无法让它发挥作用。

在外部类 MyPanel 中,我使用单独的配置启动 JPanel 并将其添加到主框架中。

for 循环仅用于测试:

class MyPanel extends JPanel {

public static JTextArea tArea;

  public MyPanel(String config) {

    JButton testButton = new JButton(config);
    add(testButton);

    tArea = new JTextArea();    
    JScrollPane scroll = new JScrollPane(tArea);
    add(scroll, BorderLayout.CENTER);  

    for (int i = 0 ; i < 50 ; i ++ ) {
        tArea.append("test \n") ;
        try { Thread.sleep(2000); } catch(InterruptedException ie) {}
    }

    setVisible(true);
    revalidate();
    repaint();

  }

}

但我认为,这是完全错误的,因为我在重新验证和重新绘制之前调用它:(并且在大型机内我想以这种方式启动它:

JMenuItem menuItem = new JMenuItem("Test");
menuItem.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent event) {
    MyPanel dynamicPanel = new MyPanel("Test");
    contentContainer.removeAll();
    contentContainer.add(dynamicPanel);
    contentContainer.revalidate();
    contentContainer.repaint();
  }
});

最佳答案

首先您需要知道,Swing 是一个单线程框架,并且不是线程安全的。这意味着与 UI 的所有更新和交互都必须在 UI 线程(事件调度线程)内进行,请参阅 Concurrency in Swing

is it a good way (or even possible) to handle multiple serverthreads on multiple sockets (not threads) inside a JTable and am i be able to send data to it to update individual rows depending on socketactivities? i mean when every row stands for a running serverthread, the user should be able to click on it, start a new one (that will be added to the list) see the socketstatus (whatever that will be) in a field, see how many clients are connected and even be able to stop a specific thread throu that table?

老实说,我认为这并不重要,您遇到的问题是将信息从线程/套接字获取到用户界面。我会考虑做的是拥有一个 Thread ,其唯一目的是轮询所有其他线程/套接字以获取信息,然后将该信息与 UI 同步,确保不会破坏 Swing 的单线程规则正在进行中。 SwingWorker 可能是一个不错的选择,具体取决于您的设计。

我不会考虑让每个线程单独向 UI 发送信息的原因是,这将是一个非常好的方法来压倒 EDT 并导致它滞后,同时它试图处理系统中的大量数据。可能会创造

when i start implementing it the way i described above, what happens to the jtable and the running threads when i update the active JPanel with another interface and switch back again? for example i have one interface for an overview about all instances and another one for details on a single instance of it. will all threads be interrupted or can they communicate to the Table after that again? or is it better to use a cardLayout?

when i change the card on a cardlayout and on this card is something running (for ex. a JTable with running socketthreads) will they be interrupted or stopped or can i update statusdata in the JTable anymore? so the sockets should be able to write and update data to the table over all.

根据我上面给出的描述,在后台运行的线程基本上不会发生任何事情,因为它们与 UI 断开连接。 “轮询”线程仍将运行,并且仍将更新发送到表(我希望通过 TableModel ),但是,因为它实际上在屏幕上不可见,所以不会被绘制和“不应该”对系统的性能产生不利影响。

这提出了某种“状态”模型的想法。该模型实际上可以用于描述单个线程的状态,然后可以由“状态”管理器进行管理,该管理器使您可以访问此信息(以及实际的线程)。轮询线程将从正在运行的线程/套接字获取信息,并更新该线程的关联“状态”模型,这意味着您可以挂起整个 UI,并知道在访问它时,它只会从内部进行修改EDT 的上下文 - 没有死锁或竞争条件。作为一个想法

And now my last question, thanks for reading so long. while trying to discover answers for all of my questions above, i tried a simple JPanel switching Frame with a JTextarea on each Panel that writes 100 lines with a 1000ms break between. then i wanted to switch the panels and see if that task was interrupted. damn i'm such a noob, i cant even bring this to work.

In an external class MyPanel i am initiating a JPanel with individual configuration and add it to the main frame.

那是因为您的代码阻塞了事件调度线程,该线程负责处理事件队列,该队列还承载重绘事件(除其他外),这意味着,在循环运行时,UI 无法无法更新(或交互)。

使用 SwingWorker 可以更好地为代码提供服务。

参见Concurrency in SwingWorker Threads and SwingWorker了解更多详情

关于java - 在多个 JPanel 上运行多个线程并切换它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28377975/

相关文章:

java - 试图从 txt 文件中填充二维 ArrayList

java - 如何有效地在 SWT/Swing 表中填充数百万条记录

java - 使用Java实现贝叶斯推理

java - 为固定宽度的长句调整对话框消息 (JOptionPane) 的大小

java - 如何使用 ITextPDF 库替换 pdf 文件中的文本?

c# - 创建并行任务,如何限制新任务创建?

multithreading - 如何在 Perl 中列出和终止线程/进程而不使用外部命令?

c++ - 指向 std::thread 的共享指针是一种不好的做法吗?

java - 如何在 Java Swing 中的 System.exit() 之前显示通知 JDialog? (关闭)

java - 为什么矩形跳跃的高度会变化?