标签:
多个生产线程,当生产到一定数量时对结果进行批量处理,触发一个消费线程将全部结果取走处理。多人往一个篮子里装东西,篮子装满时还需要接着装东西,用另外一个空篮子接着装,装满的篮子要么谁最后装满谁送到指定地点,要么叫人把篮子提走。
private List<String> nowList = new ArrayList<String>(150);
protected void put(String value)
{
List<String> fullList = null;
synchronized (this)
{
this.nowList.add(value);
/*达到100个,篮子装满提走*/
if (this.nowList.size() >= 100)
{
fullList = this.nowList;//把满的篮子移到一旁
this.nowList = new ArrayList<String>(150);//使用空篮子
}
}
if (null != fullList) {
new Thread(fullList).start();//叫人提走
}
}
标签:
原文地址:http://my.oschina.net/h2do/blog/523599