Executors.newFixedThreadPool(int);一样,它是返回线程池,启动的任务多了,其余任务就在那里等待。
这是一个简单模拟线程池的代码:EEEEE类我们把他当做线程池。
package day3_13;
import java.util.ArrayList;
import java.util.List;
public class EEEEE {
private static List<Runnable> list = new ArrayList<Runnable>();
private int size;
EEEEE(int size)
{
this.size = size;
}
public void execute(Runnable r)
{
if(list.size()<=size)
{
synchronized(EEEEE.class)
{
list.add(r);
new Thread(r).start();
}
//EEEEE.class.notifyAll();
}
{
try {
EEEEE.class.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public List<Runnable> getSize()
{
return list;
}
public static void main(String[] args) {
for(int x=0;x<10;x++)
{
new EEEEE(3).execute(new A());
}
}
}
class A implements Runnable
{
public void run()
{
//代码最后减去执行完毕那个线程
//
notifyAll();
}
}