Java中的线程组

7

我目前正在学习Java中的线程基础知识,并尝试编写一个简单的线程组程序。虽然我按照教程网站上的方式编写了它,但我得到了不同类型的输出。以下是我的代码,我得到了不同的输出结果。

public class ThreadGroupDemo implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        // get the name of the current thread.
    }

    public static void main(String[] args) {
        ThreadGroupDemo runnable = new ThreadGroupDemo();
        ThreadGroup tg1 = new ThreadGroup("Parent Group");
        // Creating thread Group.

        Thread t1 = new Thread(tg1, new ThreadGroupDemo(), "one");
        t1.start();
        t1.setPriority(Thread.MAX_PRIORITY);

        Thread t2 = new Thread(tg1, new ThreadGroupDemo(), "second");
        t2.start();
        t2.setPriority(Thread.NORM_PRIORITY);

        Thread t3 = new Thread(tg1, new ThreadGroupDemo(), "Three");
        t3.start();

        System.out.println("Thread Group name : " + tg1.getName());
        tg1.list();
    }

}

我正在获取 输出为:
Thread Group name : Parent Group
Three
java.lang.ThreadGroup[name=Parent Group,maxpri=10]
    second
one
Thread[one,10,Parent Group]
    Thread[second,5,Parent Group]
    Thread[Three,5,Parent Group]

输出应该像这样:
one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
    Thread[one,5,Parent ThreadGroup]
    Thread[two,5,Parent ThreadGroup]
    Thread[three,5,Parent ThreadGroup] 

我不明白为什么会发生这种情况?设置优先级能够帮助解决吗?


答案很简单:教程是错误的。唯一可以保证的是线程组名称将出现在线程组列表之前。 - biziclop
5
好的,我会尽力为您翻译该网页上的内容。以下是原文及翻译:ThreadGroup in Java Java中的线程组A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread group creates a tree in which every thread group except the initial thread group has a parent.线程组表示一组线程。此外,一个线程组可以包含其它线程组。线程组创建了一棵树,在其中除了初始线程组以外的每个线程组都有一个父线程组。The class ThreadGroup provides a convenient way to group multiple threads into a single object. ThreadGroup类提供了一种将多个线程组合成一个单一对象的方便方法。Constructors of ThreadGroup class: ThreadGroup类的构造函数:ThreadGroup(String name): creates a new ThreadGroup with the given name. ThreadGroup(String name):使用给定的名称创建一个新的ThreadGroup。ThreadGroup(ThreadGroup parent, String name): creates a new ThreadGroup with the given parent and name. ThreadGroup(ThreadGroup parent, String name):使用给定的父级和名称创建一个新的ThreadGroup。Methods of ThreadGroup class: ThreadGroup类的方法:
  1. public int activeCount(): returns the number of active threads in this thread group.
public int activeCount():返回此线程组中活动线程的数量。
  1. public int activeGroupCount(): returns an estimate of the number of active groups in this thread group.
public int activeGroupCount():返回此线程组中活动组的数量的估计值。
  1. public int getMaxPriority(): returns the maximum priority of this thread group.
public int getMaxPriority():返回此线程组的最大优先级。
  1. public String getName(): returns the name of this thread group.
public String getName():返回此线程组的名称。
  1. public ThreadGroup getParent(): returns the parent of this thread group.
public ThreadGroup getParent():返回此线程组的父级。
  1. public boolean isDaemon(): tests if this thread group is a daemon thread group.
public boolean isDaemon():测试此线程组是否为守护线程组。
  1. public boolean isDestroyed(): tests if this thread group has been destroyed.
public boolean isDestroyed():测试此线程组是否已被销毁。
  1. public void setDaemon(boolean daemon): changes the daemon status of this thread group.
public void setDaemon(boolean daemon):更改此线程组的守护程序状态。
  1. public void setMaxPriority(int pri): sets the maximum priority of this thread group.
public void setMaxPriority(int pri):设置此线程组的最大优先级。
  1. public void interrupt(): interrupts all threads in this thread group.
public void interrupt():中断此线程组中的所有线程。
  1. public void list(): prints information about this thread group to the standard output.
public void list():将有关此线程组的信息打印到标准输出。
  1. protected void finalize(): destroys this thread group and all its subgroups (if any).
protected void finalize():销毁此线程组及其所有子组(如果有)。Example: 示例:public class Test extends Thread { public static void main(String args[]) throws Exception { ThreadGroup g1 = new ThreadGroup("Parent ThreadGroup"); Thread t1 = new Thread(g1, "thread 1"); Thread t2 = new Thread(g1, "thread 2"); g1.setMaxPriority(3); ThreadGroup g2 = new ThreadGroup(g1, "Child ThreadGroup"); Thread t3 = new Thread(g2, "thread 3"); System.out.println("Thread Group Name: " + g1.getName()); g1.list(); } }Output: 输出:Thread Group Name: Parent ThreadGroup java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=3] Thread[thread 1,5,Parent ThreadGroup] Thread[thread 2,5,Parent ThreadGroup] java.lang.ThreadGroup[name=Child ThreadGroup,maxpri=3] Thread[thread 3,5,Child ThreadGroup]In this example, we have created two ThreadGroups: "Parent ThreadGroup" and "Child ThreadGroup". We have also created three threads: "thread 1", "thread 2", and "thread 3". "thread 1" and "thread
- Jaimin Patel
4
很震惊。我建议您使用官方教程,并忘记线程组,他们没有什么用处。设置线程的优先级也是如此。 - biziclop
1
可能是Java多线程-线程优先级的重复问题。 - Petter Friberg
1个回答

3
即使设置了优先级,您也无法预测线程执行的顺序。您对调度没有控制权,这由操作系统决定。关于Java并发的好书:Java concurrency in practice

此外,即使是家用电脑现在也有多个核心,能够同时运行多个线程。这使得新启动的线程更有可能立即被调度运行。 - biziclop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接