Java GUI 和 SwingWorker 类的多个实例

发布于 2024-08-16 17:20:56 字数 163 浏览 7 评论 0原文

我正在使用 Java 制作 GUI(谁不是?)。我知道 Swing Worker 类可以在后台进行计算以防止 GUI 挂起,但我想知道这里是否有太多的好处......

例如,如果运行的后台线程的实例太多,它会根据计算机影响程序的性能吗?一个简单但重要的问题。我将非常感谢任何意见。感谢您抽出时间。

I'm making a GUI using Java (who isn't?). I know the Swing Worker class enables computation in the background to prevent the GUI from hanging, but I was wondering if there could be too much of a good thing here...

For instance, if there were too many instances of those background threads running, would it impact the performance of the program depending on the computer? A simple, but important question. I would greatly appreciate any input. Thanks for your time.

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

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

发布评论

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

评论(3

机场等船 2024-08-23 17:20:56

是的,处理的越多,自然会影响计算机的性能。毕竟,处理资源是有限的资源。

也就是说,许多现代计算机都具有多核,这意味着单线程应用程序可能无法充分利用处理器资源。

一般来说,运行几个线程不会是一个大问题。但是,一旦有数百或数千个线程,性能就会下降,因为执行 context 所需的时间线程之间的切换可能会开始占用大部分可用处理资源。

Yes, the more processing you do, naturally, it will affect the performance of the computer. After all, processing resources is a limited resource.

That said, many modern computers have multiple cores, which means that a single-threaded application will probably not be able to take full advantage of the processor resources.

In general, having a few threads running is not going to be a big problem. However, once there are hundreds or thousands of threads, performance can degrade, as the time it takes to do a context switch between threads can start to take up a larger fraction of the processing resources that are available.

岁吢 2024-08-23 17:20:56

如果您深入研究 SwingWorker 代码,您将看到定义了以下常量:

/**
 * number of worker threads.
 */
private static final int MAX_WORKER_THREADS = 10;

因此,无论 SwingWorker 的数量有多少,后台线程的数量永远不会超过此值你实际上创造了。

改变后台线程模型的一种方法是将您自己的 ExecutorService 插入与 SwingWorker 类关联的 AppContext 中。然而,考虑到 AppContext 属于 sun.awt,因此这不是官方 JDK API 的一部分,这有点不可靠。

// Create single thread executor to force all background tasks to run on the same thread.
ExecutorService execService = Executors.newSingleThreadExecutor();

// Retrieve the AppContext.  *CAUTION*: This is part of the sun.awt package.
AppContext ctxt = AppContext.getAppContext();

// Verify that nothing is already associated with SwingWorker.class within the context.
Object obj = ctxt.get(SwingWorker.class);

if (obj != null) {
  throw new IllegalStateException("Object already associated with SwingWorker: " + obj);
}

// Install ExecutorService.  Will be retrieved by the SwingWorker when execute() is called.
ctxt.put(SwingWorker.class, ctxt);

If you delve into the SwingWorker code you'll see the following constant defined:

/**
 * number of worker threads.
 */
private static final int MAX_WORKER_THREADS = 10;

Hence, the number of background threads can never exceed this value irrespective of the number of SwingWorker's you actually create.

One way to vary the background threading model would be to plug your own ExecutorService into the AppContext associated with the SwingWorker class. However, this is slightly dodgy given that AppContext belongs to sun.awt and hence is not part of the official JDK API.

// Create single thread executor to force all background tasks to run on the same thread.
ExecutorService execService = Executors.newSingleThreadExecutor();

// Retrieve the AppContext.  *CAUTION*: This is part of the sun.awt package.
AppContext ctxt = AppContext.getAppContext();

// Verify that nothing is already associated with SwingWorker.class within the context.
Object obj = ctxt.get(SwingWorker.class);

if (obj != null) {
  throw new IllegalStateException("Object already associated with SwingWorker: " + obj);
}

// Install ExecutorService.  Will be retrieved by the SwingWorker when execute() is called.
ctxt.put(SwingWorker.class, ctxt);
花开雨落又逢春i 2024-08-23 17:20:56

这取决于您拥有的核心数量。
例如,如果您有两个核心,您可以安全地生成一个后台任务,因为您生成两个线程,一个用于 UI,另一个用于任务,这将很好地在两个核心之间分配。

然而,UI 任务通常是空闲的(等待用户执行某些操作)。因此,在 99% 的情况下,在双核计算机上,您可以生成两个工作线程(除了始终存在的标准 UI 线程之外)

It depends on the number of cores you have.
For instance, if you have two cores you can safely spawn one background task because you spawn two threads, one for the UI and one for the task, which will nicely split among the two cores.

However, the UI task is often idle (waiting for the user to do something). Thus in 99% of the cases, on a two core machine you can spawn two worker threads (in addition to the standard UI thread that is always there)

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