ThreadGroup 創建一組線程。它提供了一種將線程組作為一個單元進行管理的便捷方法。這在您想要掛起和恢複多個相關線程的情況下特別有價值。
- 線程組形成一棵樹,其中除了初始線程組之外的每個線程組都有一個父線程組。
- 允許線程訪問有關其自己的線程組的信息,但不允許訪問有關其線程組的父線程組或任何其他線程組的信息。
構造函數:
-
公共線程組(字符串名稱):構造一個新的線程組。這個新組的父級是當前正在運行的線程的線程組。
Throws: SecurityException - if the current thread cannot create a thread in the specified thread group.
-
公共線程組(線程組父級,字符串名稱):創建一個新的線程組。這個新組的父級是指定的線程組。
Throws: NullPointerException - if the thread group argument is null. SecurityException - if the current thread cannot create a thread in the specified thread group.
方法
-
int activeCount():此方法返回組中的線程數以及該線程為其父級的任何組。
Syntax: public int activeCount() Returns: This method returns an estimate of the number of active threads in this thread group and in any other thread group that has this thread group as an ancestor. Exception: NA
// Java code illustrating activeCount() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 1000; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } } } public class ThreadGroupDemo { public static void main(String arg[]) { // creating the thread group ThreadGroup gfg = new ThreadGroup("parent thread group"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); // checking the number of active thread System.out.println("number of active thread: " + gfg.activeCount()); } }
輸出:
Starting one Starting two number of active thread: 2
-
int activeGroupCount():此方法返回該線程組中活動組數量的估計值。
Syntax: public int activeGroupCount(). Returns: Returns the number of groups for which the invoking thread is parent. Exception: NA.
// Java code illustrating activeGroupCount() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 1000; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException { // creating the thread group ThreadGroup gfg = new ThreadGroup("gfg"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); // checking the number of active thread System.out.println("number of active thread group: " + gfg.activeGroupCount()); } }
輸出:
Starting one Starting two number of active thread group: 2 one finished executing two finished executing
-
無效checkAccess():使安全管理器驗證調用線程是否可以訪問和/或更改所在的組checkAccess()叫做。
Syntax: final void checkAccess(). Returns: NA. Exception: NA.
// Java code illustrating checkAccess() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 1000; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); gfg.checkAccess(); System.out.println(gfg.getName() + " has access"); gfg_child.checkAccess(); System.out.println(gfg_child.getName() + " has access"); } }
輸出:
Starting one Starting two Parent thread has access child thread has access one finished executing two finished executing
-
無效destroy():銷毀線程組以及調用該線程組的任何子組。
Syntax: public void destroy(). Returns: NA. Exception: IllegalThreadStateException - if the thread group is not empty or if the thread group has already been destroyed. SecurityException - if the current thread cannot modify this thread group.
// Java code illustrating destroy() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); // block until other thread is finished t1.join(); t2.join(); // destroying child thread gfg_child.destroy(); System.out.println(gfg_child.getName() + " destroyed"); // destroying parent thread gfg.destroy(); System.out.println(gfg.getName() + " destroyed"); } }
輸出:
Starting one Starting two child thread destroyed Parent thread destroyed
-
int枚舉(線程組[]):組成調用線程組的線程被放入組數組中。
Syntax: public int enumerate(Thread group[]). Returns: the number of threads put into the array. Exception: SecurityException - if the current thread does not have permission to enumerate this thread group.
// Java code illustrating enumerate() method. import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); // returns the number of threads put into the array Thread[] group = new Thread[gfg.activeCount()]; int count = gfg.enumerate(group); for (int i = 0; i < count; i++) { System.out.println("Thread " + group[i].getName() + " found"); } } }
輸出:
Starting one Starting two Thread one found Thread two found one finished executing two finished executing
-
int 枚舉(線程[]組,布爾遞歸):組成調用線程組的線程被放入組數組中。如果一切都是真的,那麽該線程的所有子組中的線程也被放入組中。
Syntax: public int enumerate(Thread[] list, boolean recurse). Returns: the number of threads placed into the array. Exception: SecurityException - if the current thread does not have permission to enumerate this thread group.
// Java code illustrating enumerate(Thread[] group, boolean recurse) import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); // returns the number of threads put into the array Thread[] group = new Thread[gfg.activeCount()]; int count = gfg.enumerate(group, true); for (int i = 0; i < count; i++) { System.out.println("Thread " + group[i].getName() + " found"); } } }
輸出:
Starting one Starting two Thread one found Thread two found one finished executing two finished executing
-
int枚舉(ThreadGroup[]組):調用線程組的子組被放入組數組中。
Syntax: public int enumerate(ThreadGroup[] group). Returns: the number of thread groups put into the array. Exception: SecurityException - if the current thread does not have permission to enumerate this thread group.
// Java code illustrating enumerate(ThreadGroup[] group) method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); // returns the number of threads put into the array ThreadGroup[] group = new ThreadGroup[gfg.activeCount()]; int count = gfg.enumerate(group); for (int i = 0; i < count; i++) { System.out.println("ThreadGroup " + group[i].getName() + " found"); } } }
輸出:
Starting one Starting two ThreadGroup child thread found two finished executing one finished executing
-
int enumerate(ThreadGroup[] group, boolean all):調用線程組的子組被放入組數組中。如果一切都是真的,則子組的所有子組(以此類推)也被放入組中。
Syntax: public int enumerate(ThreadGroup[] group, boolean all) Returns: the number of thread groups put into the array. Exception: SecurityException - if the current thread does not have permission to enumerate this thread group.
// Java code illustrating enumerate(ThreadGroup[] group, boolean all) import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); // returns the number of threads put into the array ThreadGroup[] group = new ThreadGroup[gfg.activeCount()]; int count = gfg.enumerate(group, true); for (int i = 0; i < count; i++) { System.out.println("ThreadGroup " + group[i].getName() + " found"); } } }
輸出:
Starting one Starting two ThreadGroup child thread found two finished executing one finished executing
-
int getMaxPriority():返回組的最大優先級設置。
Syntax: final int getMaxPriority(). Returns: the maximum priority that a thread in this thread group can have. Exception: NA.
// Java code illustrating getMaxPriority() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); // checking the maximum priority of parent thread System.out.println("Maximum priority of ParentThreadGroup = " + gfg.getMaxPriority()); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting one"); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting two"); } }
輸出:
Maximum priority of ParentThreadGroup = 10 Starting one Starting two two finished executing one finished executing
-
字符串getName():此方法返回組的名稱。
Syntax: final String getName(). Returns: the name of this thread group. Exception: NA.
// Java code illustrating getName() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); } }
輸出:
Starting one Starting two two finished executing one finished executing
-
ThreadGroup getParent():如果調用的 ThreadGroup 對象沒有父對象,則返回 null。否則,它返回調用對象的父對象。
Syntax: final ThreadGroup getParent(). Returns: the parent of this thread group. The top-level thread group is the only thread group whose parent is null. Exception: SecurityException - if the current thread cannot modify this thread group.
// Java code illustrating getParent() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Exception encounterted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); // prints the parent ThreadGroup // of both parent and child threads System.out.println("ParentThreadGroup for " + gfg.getName() + " is " + gfg.getParent().getName()); System.out.println("ParentThreadGroup for " + gfg_child.getName() + " is " + gfg_child.getParent().getName()); } }
輸出:
Starting one Starting two ParentThreadGroup for Parent thread is main ParentThreadGroup for child thread is Parent thread one finished executing two finished executing
-
無效interrupt():調用interrupt()組中所有線程的方法。
Syntax: public final void interrupt(). Returns: NA. Exception: SecurityException - if the current thread is not allowed to access this thread group or any of the threads in the thread group.
// Java code illustrating interrupt() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); // interrupting thread group gfg.interrupt(); } }
輸出:
Starting one Starting two Thread two interrupted Thread one interrupted one finished executing two finished executing
-
布爾值isDaemon():測試該線程組是否為守護線程組。當守護程序線程組的最後一個線程停止或最後一個線程組被銷毀時,守護程序線程組將自動銷毀。
Syntax: public final boolean isDaemon(). Returns: true if the group is daemon group. Otherwise it returns false. Exception: NA.
// Java code illustrating isDaemon() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); if (gfg.isDaemon() == true) System.out.println("Group is Daemon group"); else System.out.println("Group is not Daemon group"); } }
輸出:
Starting one Starting two Group is not Daemon group two finished executing one finished executing
-
布爾值isDestroyed():該方法測試該線程組是否已被銷毀。
Syntax: public boolean isDestroyed(). Returns: true if this object is destroyed. Exception: NA.
// Java code illustrating isDestroyed() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException, Exception { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); if (gfg.isDestroyed() == true) System.out.println("Group is destroyed"); else System.out.println("Group is not destroyed"); } }
輸出:
Starting one Starting two Group is not destroyed one finished executing two finished executing
-
無效list():顯示有關組的信息。
Syntax: public void list(). Returns: NA. Exception: NA.
// Java code illustrating list() method. import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException, Exception { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); // listing contents of parent ThreadGroup System.out.println("\nListing parentThreadGroup: " + gfg.getName() + ":"); // prints information about this thread group // to the standard output gfg.list(); } }
輸出:
Starting one Starting two Listing parentThreadGroup: Parent thread: java.lang.ThreadGroup[name=Parent thread, maxpri=10] Thread[one, 5, Parent thread] Thread[two, 5, Parent thread] java.lang.ThreadGroup[name=child thread, maxpri=10] one finished executing two finished executing
-
布爾parentOf(線程組組):此方法測試該線程組是否是線程組參數或其祖先線程組之一。
Syntax: final boolean parentOf(ThreadGroup group). Returns: true if the invoking thread is the parent of group(or group itself). Otherwise, it returns false. Exception: NA.
// Java code illustrating parentOf() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException, Exception { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); // checking who is parent thread if (gfg.parentOf(gfg_child)) System.out.println(gfg.getName() + " is parent of " + gfg_child.getName()); } }
輸出:
Starting one Starting two Parent thread is parent of child thread two finished executing one finished executing
-
無效setDaemon(布爾isDaemon):該方法改變該線程組的守護進程狀態。當守護程序線程組的最後一個線程停止或最後一個線程組被銷毀時,守護程序線程組將自動銷毀。
Syntax: final void setDaemon(boolean isDaemon). Returns: If isDaemon is true, then the invoking group is flagged as a daemon group. Exception: SecurityException - if the current thread cannot modify this thread group.
// Java code illustrating setDaemon() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException, Exception { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); // daemon status is set to true gfg.setDaemon(true); // daemon status is set to true gfg_child.setDaemon(true); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); if (gfg.isDaemon() && gfg_child.isDaemon()) System.out.println("Parent Thread group and " + "child thread group" + " is daemon"); } }
輸出:
Starting one Starting two Parent Thread group and child thread group is daemon one finished executing two finished executing
-
void setMaxPriority(int 優先級):設置調用組的最大優先級為priority。
Syntax: final void setMaxPriority(int priority). Returns: NA. Exception: SecurityException - if the current thread cannot modify this thread group.
// Java code illustrating setMaxPriority() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " [priority = " + Thread.currentThread().getPriority() + "] finished executing."); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException, Exception { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); gfg.setMaxPriority(Thread.MAX_PRIORITY - 2); gfg_child.setMaxPriority(Thread.NORM_PRIORITY); NewThread t1 = new NewThread("one", gfg); t1.setPriority(Thread.MAX_PRIORITY); System.out.println("Starting " + t1.getName()); t1.start(); NewThread t2 = new NewThread("two", gfg_child); t2.setPriority(Thread.MAX_PRIORITY); System.out.println("Starting " + t2.getName()); t2.start(); } }
輸出:
Starting one Starting two two [priority = 5] finished executing. one [priority = 8] finished executing.
-
字符串toString():此方法返回此線程組的字符串表示形式。
Syntax: public String toString(). Returns: String equivalent of the group. Exception: SecurityException - if the current thread cannot modify this thread group.
// Java code illustrating toString() method import java.lang.*; class NewThread extends Thread { NewThread(String threadname, ThreadGroup tgob) { super(tgob, threadname); start(); } public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(10); } catch (InterruptedException ex) { System.out.println("Thread " + Thread.currentThread().getName() + " interrupted"); } } System.out.println(Thread.currentThread().getName() + " finished executing"); } } public class ThreadGroupDemo { public static void main(String arg[]) throws InterruptedException, SecurityException, Exception { // creating the thread group ThreadGroup gfg = new ThreadGroup("Parent thread"); ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread"); // daemon status is set to true gfg.setDaemon(true); // daemon status is set to true gfg_child.setDaemon(true); NewThread t1 = new NewThread("one", gfg); System.out.println("Starting " + t1.getName()); NewThread t2 = new NewThread("two", gfg); System.out.println("Starting " + t2.getName()); // string equivalent of the parent group System.out.println("String equivalent: " + gfg.toString()); } }
輸出:
Starting one Starting two String equivalent: java.lang.ThreadGroup[name=Parent thread, maxpri=10] one finished executing two finished executing
相關用法
- Java Java.lang.ThreadGroup.activeCount()用法及代碼示例
- Java Java.lang.ThreadGroup.checkAccess()用法及代碼示例
- Java Java.lang.ThreadGroup.destroy()用法及代碼示例
- Java Java.lang.ThreadGroup.enumerate()用法及代碼示例
- Java Java.lang.ThreadGroup.getMaxPriority()用法及代碼示例
- Java Java.lang.ThreadGroup.getName()用法及代碼示例
- Java Java.lang.ThreadGroup.getParent()用法及代碼示例
- Java Java.lang.ThreadGroup.interrupt()用法及代碼示例
- Java Java.lang.ThreadGroup.isDaemon()用法及代碼示例
- Java Java.lang.ThreadGroup.isDestroyed()用法及代碼示例
- Java Java.lang.ThreadGroup.list()用法及代碼示例
- Java Java.lang.ThreadGroup.parentOf()用法及代碼示例
- Java Java.lang.ThreadGroup.setDaemon()用法及代碼示例
- Java Java.lang.ThreadGroup.setMaxPriority()用法及代碼示例
- Java Java.lang.ThreadGroup.toString()用法及代碼示例
- Java Java.lang.ThreadGroup.uncaughtException()用法及代碼示例
- Java Java.lang.Thread.activeCount()用法及代碼示例
- Java Java.lang.Thread.checkAccess()用法及代碼示例
- Java Java.lang.Thread.currentThread()用法及代碼示例
- Java Java.lang.Thread.dumpStack()用法及代碼示例
- Java Java.lang.Thread.enumerate()用法及代碼示例
- Java Java.lang.Thread.getAllStackTraces()用法及代碼示例
- Java Java.lang.Thread.getContextClassLoader()用法及代碼示例
- Java Java.lang.Thread.getId()用法及代碼示例
- Java Java.lang.Thread.getName()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.lang.ThreadGroup class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。