//程序会停在join那里不动,这是为什么啊?(好像是哪个线程不能结束?这是为什么啊!)
public class ThreadGroupDemo
{public static void main(String args[])
{
ThreadGroup groupA=new ThreadGroup("groupA");
ThreadGroup groupB=new ThreadGroup("groupB");newThread ob1=new newThread("one",groupA);
newThread ob2=new newThread("Two",groupA);
newThread ob3=new newThread("Three",groupB);
newThread ob4=new newThread("Four",groupB);System.out.println("\nHere is output from list():");
groupA.list();//LIST显示关于线程组的信息
groupB.list();
System.out.println();System.out.println("Suspending Group A");
Thread tga[]=new Thread[groupA.activeCount()];//activeCount返回线程组加上以这个线程作为父类的所有线程组中线程的个数
groupA.enumerate(tga);//enumerate将构成调用线程组的线程放入tga数组中
for(int i=0;i<tga.length;++i)
{
((newThread)tga[i]).mysuspend();//暂停每一个线程
}
try
{
Thread.sleep(4000);
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}System.out.println("Resuming Group A");
for(int i=0;i<tga.length;++i)
{
((newThread)tga[i]).myresume();
}
System.out.println("Waiting for threads to finish.");
try
{
ob1.join();
ob2.join();
ob3.join();
ob4.join();
}
catch(InterruptedException e)
{
System.out.println("Exception in main thread");
}
System.out.println("Main thread exiting");
}
}
class newThread extends Thread
{
boolean suspendFlag;
newThread(String threadname,ThreadGroup tgOb)
{
super(tgOb,threadname);
System.out.println("New Thread:"+this);
suspendFlag=false;
start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println(getName()+":"+i);
Thread.sleep(1000);
synchronized(this)
{
while(suspendFlag=true)
{
wait();
}
}
}
}
catch(Exception e)
{
System.out.println("Exception in "+getName());
}
System.out.println(getName()+"exiting");
}
public void mysuspend()
{
suspendFlag=true;
}
public synchronized void myresume()
{
suspendFlag=false;
notify();
}
}
public class ThreadGroupDemo
{public static void main(String args[])
{
ThreadGroup groupA=new ThreadGroup("groupA");
ThreadGroup groupB=new ThreadGroup("groupB");newThread ob1=new newThread("one",groupA);
newThread ob2=new newThread("Two",groupA);
newThread ob3=new newThread("Three",groupB);
newThread ob4=new newThread("Four",groupB);System.out.println("\nHere is output from list():");
groupA.list();//LIST显示关于线程组的信息
groupB.list();
System.out.println();System.out.println("Suspending Group A");
Thread tga[]=new Thread[groupA.activeCount()];//activeCount返回线程组加上以这个线程作为父类的所有线程组中线程的个数
groupA.enumerate(tga);//enumerate将构成调用线程组的线程放入tga数组中
for(int i=0;i<tga.length;++i)
{
((newThread)tga[i]).mysuspend();//暂停每一个线程
}
try
{
Thread.sleep(4000);
}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}System.out.println("Resuming Group A");
for(int i=0;i<tga.length;++i)
{
((newThread)tga[i]).myresume();
}
System.out.println("Waiting for threads to finish.");
try
{
ob1.join();
ob2.join();
ob3.join();
ob4.join();
}
catch(InterruptedException e)
{
System.out.println("Exception in main thread");
}
System.out.println("Main thread exiting");
}
}
class newThread extends Thread
{
boolean suspendFlag;
newThread(String threadname,ThreadGroup tgOb)
{
super(tgOb,threadname);
System.out.println("New Thread:"+this);
suspendFlag=false;
start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println(getName()+":"+i);
Thread.sleep(1000);
synchronized(this)
{
while(suspendFlag=true)
{
wait();
}
}
}
}
catch(Exception e)
{
System.out.println("Exception in "+getName());
}
System.out.println(getName()+"exiting");
}
public void mysuspend()
{
suspendFlag=true;
}
public synchronized void myresume()
{
suspendFlag=false;
notify();
}
}
解决方案 »
- 请问高手 正则表达式以及类初始化问题
- 请教下在FileInputStream如何设置相对路径,谢谢!
- 常量池的问题
- 连接数据库异常的问题。
- 在JFileChooser里双击.lnk文件无法转移到目标文件夹!
- 谁有现成的,整理好的生成连接池的类,以及怎样使用该类,先谢了
- 把一个字符串以回车换行、TAB键split,要怎么写呢?请知道的靓哥帅妹指教 :)
- 从map 中取出来的键值对 值是对象,如何转化成具体的对象,key不确定
- 一个简单的Java程序!错误不知道是怎么产生的,请大家指点!谢谢!
- 请问java初学者(自学)没项目经验,能否找到java编程工作?如果能,待遇如何?
- 关于数据库中表的主键的生成,大家有没有什么好的方法?
- *****初学者的问题,java多线程通信的问题******
while(suspendFlag=true){
wait();
}
改成
while(suspendFlag == true){
wait();
}
即可正常运行。