java

位置:IT落伍者 >> java >> 浏览文章

Java-多线程组的控制


发布日期:2019年02月04日
 
Java-多线程组的控制

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);

}

}

               

上一篇:Java多线程问题及处理(笔记)

下一篇:Java程序中通用的体系模式 windancery