class NewThread extends Thread{
boolean suspendFlag;
NewThread(String threadname ThreadGroup tgOb){
super(tgOb threadname);
Systemoutprintln(New thread: +this);
suspendFlag = false;
start();
}
public void run(){
try{
for(int i=;i>;i){
Systemoutprintln(getName()+: +i);
Threadsleep();
synchronized(this){
while(suspendFlag){
wait();
}
}
}
}catch(Exception e){
Systemoutprintln(Exception in +getName());
}
Systemoutprintln(getName()+exiting);
}
void mysuspend(){
suspendFlag = true;
}
synchronized void myresume(){
suspendFlag = false;
notify();
}
}
public class test {
public static void main(String[] args) {
// TODO Autogenerated method stub
ThreadGroup groupA = new ThreadGroup(Group A);
ThreadGroup groupB = new ThreadGroup(Group B);
NewThread ob = new NewThread(one groupA);
NewThread ob = new NewThread(Tow groupA);
NewThread ob = new NewThread(three groupB);
NewThread ob = new NewThread(Four groupB);
Systemoutprintln(Suspending Group A);
Thread tga[] = new Thread[groupAactiveCount()];
groupAenumerate(tga);
for(int i=;i<tgalength;i++){
((NewThread)tga[i])mysuspend();
}
try {
Threadsleep();
}catch(InterruptedException e){
Systemoutprintln(Main thread interrupted);
}
Systemoutprintln(Resuming Group A);
for(int i=;i<tgalength;i++){
((NewThread)tga[i])myresume();
}
try {
Systemoutprintln(Waiting for threads to finish);
objoin();
objoin();
objoin();
objoin();
}catch (Exception e){
Systemoutprintln(Exception in Main thread);
}
Systemoutprintln(Main thread exiting);
}
}