ThreadGroup
class provides interrupt method, to interrupt all the threads in a
group.
public
final void interrupt()
Interrupts
all threads in this thread group and its sub groups.
Example
class InterruptEx extends Thread{ public void run(){ while(!isInterrupted()){ } System.out.println(Thread.currentThread().getName() + " Interrupted"); } InterruptEx(ThreadGroup grp, String name){ super(grp, name); } public static void main(String args[])throws Exception{ ThreadGroup group1 = new ThreadGroup("group1"); ThreadGroup group2 = new ThreadGroup(group1, "group2"); InterruptEx t1 = new InterruptEx(group1, "Thread1Group1"); InterruptEx t2 = new InterruptEx(group1, "Thread2Group1"); InterruptEx t3 = new InterruptEx(group1, "Thread3Group2"); InterruptEx t4 = new InterruptEx(group1, "Thread4Group2"); t1.start(); t2.start(); t3.start(); t4.start(); group1.interrupt(); t1.join(); t2.join(); t3.join(); t4.join(); } }
Sample
Output
Thread1Group1 Interrupted Thread4Group2 Interrupted Thread3Group2 Interrupted Thread2Group1 Interrupted
No comments:
Post a Comment