我对多线程很陌生,所以我要问的问题可能很简单。
在我的程序中,有两个主题,一个是主主题,第二个是 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 的主题组更改为我自己而不是主。 我怎么能这样做?
使用什么Java方法来更改线程的线程组?
通过更改线程组会有任何问题吗?
答案 0 :(得分: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();
}
}
}
}
答案 1 :(得分:4)
您应该创建自己的ThreadGroup并使用Thread(ThreadGroup组,String name)构造函数来创建一个Thread:
class MyThread extends Thread {
public MyThread(ThreadGroup tg, String name) {
super(tg, name);
start();
}
...
ThreadGroup tg = new ThreadGroup("mythreadgroup");
new MyThread(tg, "mythread");
答案 2 :(得分: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()
和限制设置给线程的优先级。
但是,如果您只想更改MyThread的toString()
,那么您可以简单地覆盖toString() - 这样会更有用。
答案 3 :(得分:0)
更多信息:
ThreadGroup是Java中的一个类,Java中的每个线程都有一些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]
}
}