2013-07-16 30 views
1

我對多線程很陌生,所以我要問的問題可能很簡單。如何在Java中更改線程組的名稱?

在我的節目中,有兩個線程,一個是線程,第二個是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方法來更改線程的線程組?

將有任何問題通過更改線程組?

+2

也不會從線程的構造函數調用'start()'..線程的用戶可能不知道這樣一個事實,即構建MyThread實際啓動它! – sanbhat

回答

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(); 
      } 
     } 
    } 
} 
3

您應該創建自己的線程組,並使用線程(線程組組,字符串名稱)構造函數來創建線程:

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

... 

ThreadGroup tg = new ThreadGroup("mythreadgroup"); 
new MyThread(tg, "mythread"); 
1

主題有一個構造函數,它允許你設置一個線程組:

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() - 這會更有用。

相關問題