import java.util.*;
class testTemp
{
public static void main(String args[])
{
ArrayList al=new ArrayList();//线程池
                  //创建线程池
for(int i=0;i<10;i++)
{

r rr=new r(al);
al.add(rr);

}

System.out.println("the pool size is "+al.size());
int n=0;
                  //调用线程来工作
while(n<500)
{
n++;
for(int i=0;i<al.size();i++)
{
if(al.size()>0)
{
                            取出线程并唤醒
r r2=(r)al.remove(0);
r2.nt();
}
continue;

}
}
try
{

Thread.sleep(1000);
}
catch(Exception e)
{
}


System.out.println("now the pool size is "+al.size());



}

}
class r implements Runnable
{
ArrayList al;//线程池的引用
boolean b=true;
public r(ArrayList al)
{
this.al=al;
Thread thread=new Thread(this);
thread.start();
}

public void run()
{
synchronized(this)
{
//不让线程停止,只有工作和等待
while(true)
{
try
{

if(b)
{
this.wait();
}

System.out.println("wake up");
}
catch(Exception thomas)
{
System.out.println(thomas);
thomas.printStackTrace();
}
//把自己返还给线程池并重新等待
al.add(this);
b=true;



}
}




}
//用于唤醒本线程
public void nt()

{

try
{
b=false;
synchronized(this)
{

this.notifyAll();
}

}
catch(Exception e)
{

}


}
}