java 怎样得到创建的所有线程

gadmyth 2008-09-16 08:46:50
程序开了2个子线程,现在要让所有的线程发送同一个消息,我就要得到所有的子线程,这个怎么搞,谢谢
...全文
234 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZangXT 2008-09-17
  • 打赏
  • 举报
回复
学习了.
sagezk 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 MyLove_xia 的回复:]
引用 4 楼 sagezk 的回复:
代码:
Java codepublic class AllThread {
...
[/Quote]

2: Reference Handler
3: Finalizer
4: Signal Dispatcher
5: Attach Listener

上面 4 个是系统级线程,Java 应用程序启动后它们会被自动创建并启动,如果使用了 AWT 或 Swing,还会有更多线程被自动创建并启动以处理窗体事件等。其中 Finalizer 是处理垃圾回收的。

1: main

这个是与 main 方法对应的主线程。
老班长涛哥 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sagezk 的回复:]
代码:

Java codepublic class AllThread {

public static Thread[] getAllThread() {
ThreadGroup root = Thread.currentThread().getThreadGroup();
ThreadGroup ttg = root;
while ((ttg = ttg.getParent()) != null) root = ttg;
Thread[] tlist = new Thread[(int)(root.activeCount() * 1.2)];
return java.util.Arrays.copyOf(tlist, root.enumerate(tlist, tru…
[/Quote]

您能不能具体说说这几个线程的关系?
shangpulaolang 2008-09-17
  • 打赏
  • 举报
回复
好贴
老班长涛哥 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qj123456_0 的回复:]
看看这个例子就明白了,一共三个线程。其中main线程是jvm自动启动的。

Java code
public class AllThread {

/**
* @param args
*/
public static void main(String[] args) {
new Thread("thread1") {
public void run() {
for (int i = 0; i < 2; i++) {
try {
Thread.sleep(500);
}…
[/Quote]

可以运行,结果
main
thread1
thread2
haoweishow01 2008-09-17
  • 打赏
  • 举报
回复
收藏
qj123456_0 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gadmyth 的回复:]
不行,提示说
at java.lang.ThreadGroup.enumerate(Unknown Source)
[/Quote]
是运行通不过还是编译出错?换成int count = tg.enumerate(threads, false);试试
dashi99 2008-09-17
  • 打赏
  • 举报
回复
收藏学习了。。。
qj123456_0 2008-09-17
  • 打赏
  • 举报
回复
我觉得楼主不太可能用到线程组。所以我那个程序他够用了。找出那么多系统线程对他没有作用。
sagezk 2008-09-16
  • 打赏
  • 举报
回复
代码:
public class AllThread {

public static Thread[] getAllThread() {
ThreadGroup root = Thread.currentThread().getThreadGroup();
ThreadGroup ttg = root;
while ((ttg = ttg.getParent()) != null) root = ttg;
Thread[] tlist = new Thread[(int)(root.activeCount() * 1.2)];
return java.util.Arrays.copyOf(tlist, root.enumerate(tlist, true));
}

public static void main(String[] args) {
Thread[] ts = getAllThread();
for (Thread t : ts) {
System.out.println(t.getId() + ": " + t.getName());
}
}

}

输出:
2: Reference Handler
3: Finalizer
4: Signal Dispatcher
5: Attach Listener
1: main

最好别操作前 4 个系统线程。
gadmyth 2008-09-16
  • 打赏
  • 举报
回复
不行,提示说
at java.lang.ThreadGroup.enumerate(Unknown Source)
qj123456_0 2008-09-16
  • 打赏
  • 举报
回复
看看这个例子就明白了,一共三个线程。其中main线程是jvm自动启动的。

public class AllThread {

/**
* @param args
*/
public static void main(String[] args) {
new Thread("thread1") {
public void run() {
for (int i = 0; i < 2; i++) {
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
}
}.start();

new Thread("thread2") {
public void run() {
for (int i = 0; i < 2; i++) {
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
}
}.start();

ThreadGroup tg = Thread.currentThread().getThreadGroup();
Thread[] threads = new Thread[20];
int count = tg.enumerate(threads);
for (int i = 0; i < count; i++) {
System.out.println(threads[i].getName());
}

}

}

liuzhengkang 2008-09-16
  • 打赏
  • 举报
回复
让所有的线程发送同一个消息,什么意思啊,run()方法相同不就行了吗

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧