2

我对多线程非常陌生,所以我要问的问题可能很简单。

在我的程序中,有两个线程,一个是线程,第二个是mythread

package multithreading;

public class ThreadDemo {
    public static void main(String args[]) {        
        System.out.println(Thread.currentThread());
        new MyThread();
        try {
            for(int i = 1; i<=5; i++) {                             
                Thread.sleep(1000);
            }

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

class MyThread extends Thread {
    public MyThread() {     
        start();
    }

    public void run() {
        Thread.currentThread().setName("mythread");
        System.out.println(Thread.currentThread());
        for(int i = 1; i<=5; i++) {
            try {
                Thread.sleep(500);
                //System.out.println("MyThread i value "+i);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

现在,程序的输出是,

Thread[main,5,main]
Thread[mythread,5,main]

我知道输出是什么意思。

但我想将mythread的线程组更改为我自己的而不是main。我怎样才能做到这一点?

用什么Java方法来改变一个线程的线程组?

更改线程组会有什么问题吗?

4

4 回答 4

5

我建议实现 Runnable 而不是扩展 Thread。然后,您可以轻松地使用自己的组创建一个新线程:

public class ThreadDemo {
   public static void main(String args[]) {
        System.out.println(Thread.currentThread());
        Runnable r = new MyRunnable();
        new Thread(new ThreadGroup("my group"), r).start();
        try {
            for (int i = 1; i <= 5; i++) {
                Thread.sleep(1000);
            }

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

class MyRunnable implements Runnable {

    public void run() {
        Thread.currentThread().setName("mythread");
        System.out.println(Thread.currentThread());
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(500);
                //System.out.println("MyThread i value "+i);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
于 2013-07-16T10:31:38.273 回答
4

您应该创建自己的 ThreadGroup 并使用 Thread(ThreadGroup group, String name) 构造函数来创建线程:

class MyThread extends Thread {
    public MyThread(ThreadGroup tg, String name) {
        super(tg, name);
        start();
    }

...

ThreadGroup tg = new ThreadGroup("mythreadgroup");
new MyThread(tg, "mythread");
于 2013-07-16T10:40:33.277 回答
1

Thread 有一个构造函数,允许您设置 ThreadGroup:

Thread(ThreadGroup group, String name)

因此,如果您要像这样更改构造函数:

class MyThread extends Thread {
    public MyThread(ThreadGroup group) {
        // We set the name here instead.
        super(group, "mythread");          
        start();
    }
}

然后像这样创建线程:

public class ThreadDemo {
    public static void main(String args[]) {        
        System.out.println(Thread.currentThread());
        ThreadGroup threadGroup = new ThreadGroup("mythread");
        new MyThread(threadGroup);
        try {
            for(int i = 1; i<=5; i++) {                             
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

线程组基本上是为了方便收集线程,然后能够对所有线程执行操作,例如interrupt()限制为线程设置的优先级。

但是,如果您只想更改toString()MyThread 的 ,那么您可以简单地覆盖 toString() ——这会更有用。

于 2013-07-16T10:43:35.560 回答
0

更多信息:
ThreadGroup 是 java 中的一个类,java 中的每个 Thread 都有一些 ThreadGroup,以检查 ThreadGroup 父名称的使用

Thread.currentThread().getThreadGroup().getParent().getName();

例如主线程的 ThreadGroup 是 >system 我们可以创建自己的 ThreadGroup 为

ThreadGroup threadGroup = new ThreadGroup("myCustomThreadGroup");

在上面的例子中,

public class ThreadDemo 
{
  public static void main(String args[]) 
  {
    System.out.println(Thread.currentThread().getThreadGroup().getParent().getName()); // system
    System.out.println(Thread.currentThread()); // Thread[main,5,main]
    ThreadGroup threadGroupName = new ThreadGroup("MyOwnThreadGroup"); // create ThreadGroup
    new MyThread(threadGroupName, "myCustThread1"); // create Thread   
  }
}

class MyThread extends Thread {
 MyThread(ThreadGroup threadGroupName, String threadName) 
 {
    super(threadGroupName, threadName);
    start();
 }
 public void run() 
 {
    System.out.println(Thread.currentThread()); //Thread[myCustThread1,5,MyOwnThreadGroup]
 }
}
于 2018-07-11T07:17:24.127 回答