可以运行的最大 Swing 工作线程数是多少

发布于 2024-12-19 03:05:02 字数 57 浏览 1 评论 0原文

可以运行的 Swing Worker 线程数是否有上限,或者是内存支持的上限?这也可以在某处配置吗?

Is there an upper limit on the number of Swing Worker threads that can be run or is it like as far as the memory supports? Also is this configurable somewhere?

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

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

发布评论

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

评论(2

拿命拼未来 2024-12-26 03:05:02

SwingWorker 本身不是线程,而是将在线程中执行的任务。通常,您会使用 ExecutorService 来执行 SwingWorker 的实例;该接口还允许设置线程数:

 int n = 20; // Maximum number of threads
 ExecutorService threadPool = Executors.newFixedThreadPool(n);
 SwingWorker w; //don't forget to initialize
 threadPool.submit(w);

现在,如果您提交超过 n 个 SwingWorker 实例,它们将必须排队并等待,直到池中的线程可用。

A SwingWorker is not a thread itself but a task that will be executed in a thread. Usually, you would use an ExecutorService to execute instances of SwingWorker; this interface also allows to set the number of threads:

 int n = 20; // Maximum number of threads
 ExecutorService threadPool = Executors.newFixedThreadPool(n);
 SwingWorker w; //don't forget to initialize
 threadPool.submit(w);

Now, if you submit more than n SwingWorker instances, they'll have to queue up and wait until a thread from the pool gets available.

病毒体 2024-12-26 03:05:02
final int corePoolSize = 100;
final int maximumPoolSize = 100;
final long keepAliveTime = 100000;
final TimeUnit unit = TimeUnit.SECONDS;
final BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(maximumPoolSize);
sun.awt.AppContext.getAppContext().put(SwingWorker.class,
                 new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue));

上面的代码示例将允许您执行超过默认数量的 SwingWorkers。当然,它正在访问一些后门 sun.awt.AppContext 类,但对于那些感兴趣但无法/不愿意提供自己的 ExecutorService 的人来说,这是一个快速解决方法。

final int corePoolSize = 100;
final int maximumPoolSize = 100;
final long keepAliveTime = 100000;
final TimeUnit unit = TimeUnit.SECONDS;
final BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(maximumPoolSize);
sun.awt.AppContext.getAppContext().put(SwingWorker.class,
                 new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue));

The above code sample will allow you to have more than the default number of SwingWorkers executing. Of course it is accessing some backdoor sun.awt.AppContext class, but it is a quick workaround for those interested, and not able/willing to provide their own ExecutorService.

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