Java中的线程组是什么? [英] What are Thread Groups in Java?

查看:47
本文介绍了Java中的线程组是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么互联网上关于线程组的文档如此之少?它们仍在使用还是一些陈旧的概念?有人能解释一下吗:

I am wondering why there is so little documentation about Thread Groups on the internet ? Are they still used or they are some stale concepts ? Can some one explain:

  • 它们是什么.

  • What they are.

它们的用途.

如果仍然使用它们,在哪里?

If they are stilled used, where ?

给出一些真实的应用程序示例(比如 Web 服务器,也许).

Give some real application examples (web servers like, maybe).

推荐答案

它们被用作一组线程.在简单的应用程序中,您只需要一个,但在更复杂的应用程序服务器中,每个应用程序都有一个是有意义的.

They are used as a Group of Threads. In a simple application you only need one, but in a more complex application server it makes sense to have one for each application.

为什么互联网上关于线程组的文档如此之少?

why there is so little documentation about Thread Groups on the internet ?

我想有些人认为这是一个非常简单的想法.不确定它缺少什么.

I guess some assume it's a pretty simple idea. Not sure what is missing about it.

它们还在使用还是一些陈旧的概念?

Are they still used or they are some stale concept ?

我认为大多数开发人员从未考虑过线程组.但我认为它们在某些情况下很有用.我们有一个库,其中有一个用于重置线程关联的自定义 ThreadGroup.

I would image most developers never think about Thread Groups. But I think they are useful in certain situations. We have a library where we have a custom ThreadGroup for resetting thread affinity.

谁能解释一下它们的用途,如果仍然使用,并举个例子.

Can some one explain that are they what they are used for, if still used, and give an example.

大多数在应用程序服务器中,每个服务器都有自己的线程集合,可以集中管理.如果您想监控或关闭应用程序,您需要知道应用程序启动了哪些线程.

Mostly in applications servers, each server has it's own collection of threads and can be managed collectively. If you want to monitor or shutdown an application your need to know which threads the application started.

如果您在 ThreadGroup 中启动一个线程,那么它创建的每个线程也将在该线程组中.如果没有此功能,您将很难为应用程序分配线程.

If you start off a thread in a ThreadGroup, every Thread it creates will also be in that thread group. Without this feature, you would have a hard time assigning threads to applications.

来自@biziclop:您如何可靠地枚举组中的线程?

From @biziclop: How do you reliably enumerate threads in a group?

您可以获取 activeThreads 的大小并在 ThreadGroup 锁定 this 时进行枚举(无论好坏)

You can get the size of activeThreads and enumerate as the ThreadGroup locks on this (for better or worse)

synchronized(threadGroup) {
    Thread[] threads = threadGroup.activeCount();
    threadGroup.enumerate(threads);
    // use threads before the lock is released or it could be wrong.
}

这篇关于Java中的线程组是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆