通过Apache common pool开源包实现对象池
发布日期:2021-06-29 18:31:09 浏览次数:2 分类:技术文章

本文共 2992 字,大约阅读时间需要 9 分钟。

下面通过一个简单的样例来说明如何利用apache common pool来应用对象池。

假定我现在有一个任务,就是对一堆字符串进行格式化,为了加快速度,采用了多线程的方式允许,而格式化则是通过对象StringFormat来实现。

采用池技术,目的在于循环利用此对象,避免不停的生成和回收类。

也许本样例并不是很恰当,但是如何StringFormat换成是数据库连接就非常适合池技术了,此样例仅用于说明如何使用apache common pool池而已。

字符串格式化类:

public class StringFormat {	public String format(String str)	{		return "formated:"+str;	}}

对象工厂:

import org.apache.commons.pool2.BasePooledObjectFactory;import org.apache.commons.pool2.PooledObject;import org.apache.commons.pool2.impl.DefaultPooledObject;public class StringFormatFactory    extends BasePooledObjectFactory
{ @Override public StringFormat create() { System.out.println("create object"); return new StringFormat(); } /** * Use the default PooledObject implementation. */ @Override public PooledObject
wrap(StringFormat buffer) { return new DefaultPooledObject
(buffer); } /** * When an object is returned to the pool, clear the buffer. */ @Override public void passivateObject(PooledObject
pooledObject) { System.out.println("Object been returned to pool"); } // for all other methods, the no-op implementation // in BasePooledObjectFactory will suffice}

处理线程类:

import org.apache.commons.pool2.ObjectPool;public class StringProcessThread extends Thread {	private ObjectPool
pool; private String toProcessStr; public StringProcessThread(ObjectPool
pool, String toProcessStr) { this.pool = pool; this.toProcessStr = toProcessStr; } public void run() { StringFormat stringFormat = null; try { stringFormat = pool.borrowObject(); String formattedStr = stringFormat.format(toProcessStr); System.out.println(formattedStr); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stringFormat != null) { pool.returnObject(stringFormat); } } catch (Exception e) { e.printStackTrace(); } } }}

主程序:

import java.io.IOException;import java.io.Reader;import java.util.ArrayList;import java.util.List;import org.apache.commons.pool2.ObjectPool;import org.apache.commons.pool2.impl.GenericObjectPool;public class StringProcessor {	private ObjectPool
pool; public StringProcessor(ObjectPool
pool) { this.pool = pool; } /** * Dumps the contents of the {@link Reader} to a String, closing the * {@link Reader} when done. */ public void process(List
strList) { for (String str : strList) { Thread thread = new StringProcessThread(pool, str); thread.start(); } //设置等待两秒,等待线程结束 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { StringProcessor stringProcessor = new StringProcessor( new GenericObjectPool
(new StringFormatFactory())); List
strList = new ArrayList
(); strList.add("123"); strList.add("456"); strList.add("789"); stringProcessor.process(strList); }}

转载地址:https://cxyxy.blog.csdn.net/article/details/24385981 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SUSE Linux 报错:too many open files in system
下一篇:Java 多线程与并发编程专题

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月09日 10时23分31秒