当前位置: 首页 > news >正文

百色市网站建设_网站建设公司_UI设计_seo优化

建设网站的制作步骤,有个网站可以学做ppt,二手网站建设目标,平面设计类网站js 实现轻量ps对象池是包含指定数量的对象的容器。 从池中获取对象时#xff0c;在将对象放回之前#xff0c;该对象在池中不可用。 池中的对象具有生命周期#xff1a;创建#xff0c;验证#xff0c;销毁等。池有助于更好地管理可用资源。 有许多使用示例。 特别是在应用… js 实现轻量ps 对象池是包含指定数量的对象的容器。 从池中获取对象时在将对象放回之前该对象在池中不可用。 池中的对象具有生命周期创建验证销毁等。池有助于更好地管理可用资源。 有许多使用示例。 特别是在应用程序服务器中有数据源池线程池等。在以下情况下应使用池 高频使用相同的物体 对象很大消耗很多内存 对象需要很多时间进行初始化 对象使用大量的IO操作流套接字数据库等 对象不是线程安全的 当我为我的一个Java项目寻找一个池实现时我发现许多人都引用了Apache Commons Pool 。 Apache Commons Pool提供了一个对象池API。 有接口ObjectPoolObjectPoolFactoryPoolableObjectFactory和许多实现。 池提供方法addObject借款对象invalidateObjectreturnObject来添加获取移除和返回对象。 PoolableObjectFactory定义池中对象的行为并为池的操作提供各种回调。 在仔细研究实现细节之后我发现Apache Commons Pool不是轻量级的实现这对我来说是一个开销。 此外它对许多方法都使用了旧的Java关键字sync因此不建议使用许多方法。 Java 5引入了用于Java并发多线程的Executor框架。 此处最好使用Executor框架。 我决定实现一个简单且轻量级的池我想在这里介绍它。 它只是一个Java类。 我认为如果您不需要回调和其他高级功能就足够了。 我在GitHub上创建了一个项目easy-pool 。 池实现基于java.util.concurrent包中的ConcurrentLinkedQueue。 ConcurrentLinkedQueue是基于链接节点的线程安全队列。 该队列按照FIFO原理先进先出对元素进行排序。 我对通用池的实现如下所示 import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;public abstract class ObjectPoolT {private ConcurrentLinkedQueueT pool;private ScheduledExecutorService executorService;/*** Creates the pool.** param minIdle minimum number of objects residing in the pool*/public ObjectPool(final int minIdle) {// initialize poolinitialize(minIdle);}/*** Creates the pool.** param minIdle minimum number of objects residing in the pool* param maxIdle maximum number of objects residing in the pool* param validationInterval time in seconds for periodical checking of minIdle / maxIdle conditions in a separate thread.* When the number of objects is less than minIdle, missing instances will be created.* When the number of objects is greater than maxIdle, too many instances will be removed.*/public ObjectPool(final int minIdle, final int maxIdle, final long validationInterval) {// initialize poolinitialize(minIdle);// check pool conditions in a separate threadexecutorService Executors.newSingleThreadScheduledExecutor();executorService.scheduleWithFixedDelay(new Runnable(){Overridepublic void run() {int size pool.size();if (size minIdle) {int sizeToBeAdded minIdle - size;for (int i 0; i sizeToBeAdded; i) {pool.add(createObject());}} else if (size maxIdle) {int sizeToBeRemoved size - maxIdle;for (int i 0; i sizeToBeRemoved; i) {pool.poll();}}}}, validationInterval, validationInterval, TimeUnit.SECONDS);}/*** Gets the next free object from the pool. If the pool doesnt contain any objects,* a new object will be created and given to the caller of this method back.** return T borrowed object*/public T borrowObject() {T object;if ((object pool.poll()) null) {object createObject();}return object;}/*** Returns object back to the pool.** param object object to be returned*/public void returnObject(T object) {if (object null) {return;}this.pool.offer(object);}/*** Shutdown this pool.*/public void shutdown() {if (executorService ! null) {executorService.shutdown();}}/*** Creates a new object.** return T new object*/protected abstract T createObject();private void initialize(final int minIdle) {pool new ConcurrentLinkedQueueT();for (int i 0; i minIdle; i) {pool.add(createObject());}} } 抽象类ObjectPool提供了两个主要方法roweObject从池中获取下一个空闲对象returnObject将借入的对象返回池中。 如果池中不包含任何对象则将创建一个新对象并将其交还给借阅方法的调用者。 对象创建在方法createObject中进行。 任何扩展抽象类ObjectPool的类都只需实现此方法即可使用该池。 如您所见我还利用java.util.concurrent包中的ScheduledExecutorService。 这有什么用 您可以指定池中驻留的最小和最大对象数。 ScheduledExecutorService在单独的线程中启动特殊任务并在指定时间参数validationInterval中观察定期对象池中的最小和最大数量。 当对象数小于最小值时将创建丢失的实例。 当对象数大于最大值时将删除太多实例。 有时这对于平衡池中的内存消耗对象以及更多对象很有用。 让我们实现测试类以显示对具体池的使用。 首先我们需要一个表示池中对象的类该类模拟耗时的过程。 称为ExportingProcess的此类需要一些时间才能实例化。 public class ExportingProcess {private String location;private long processNo 0;public ExportingProcess(String location, long processNo) {this.location location;this.processNo processNo;// doing some time expensive calls / tasks// ...// for-loop is just for simulationfor (int i 0; i Integer.MAX_VALUE; i) {}System.out.println(Object with process no. processNo was created);}public String getLocation() {return location;}public long getProcessNo() {return processNo;} } 第二类实现Runnable接口并模拟线程执行的某些任务。 在run方法中我们借用了ExportingProcess的一个实例稍后将其返回到池中。 public class ExportingTask implements Runnable {private ObjectPoolExportingProcess pool;private int threadNo;public ExportingTask(ObjectPoolExportingProcess pool, int threadNo) {this.pool pool;this.threadNo threadNo;}public void run() {// get an object from the poolExportingProcess exportingProcess pool.borrowObject();System.out.println(Thread threadNo : Object with process no. exportingProcess.getProcessNo() was borrowed);// do something// ...// for-loop is just for simulationfor (int i 0; i 100000; i) {}// return ExportingProcess instance back to the poolpool.returnObject(exportingProcess);System.out.println(Thread threadNo : Object with process no. exportingProcess.getProcessNo() was returned);} } 现在在JUnit类TestObjectPool中我们创建一个ExportingProcess类型的对象池。 这是通过新的ObjectPool ExportingProcess4105发生的。 参数在下面的注释中描述。 import org.junit.After; import org.junit.Before; import org.junit.Test;import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong;public class TestObjectPool {private ObjectPoolExportingProcess pool;private AtomicLong processNo new AtomicLong(0);Beforepublic void setUp() {// Create a pool of objects of type ExportingProcess. Parameters:// 1) Minimum number of special ExportingProcess instances residing in the pool 4// 2) Maximum number of special ExportingProcess instances residing in the pool 10// 3) Time in seconds for periodical checking of minIdle / maxIdle conditions in a separate thread 5.// When the number of ExportingProcess instances is less than minIdle, missing instances will be created.// When the number of ExportingProcess instances is greater than maxIdle, too many instances will be removed.// If the validation interval is negative, no periodical checking of minIdle / maxIdle conditions// in a separate thread take place. These boundaries are ignored then.pool new ObjectPoolExportingProcess(4, 10, 5){protected ExportingProcess createObject() {// create a test object which takes some time for creationreturn new ExportingProcess(/home/temp/, processNo.incrementAndGet());}};}Afterpublic void tearDown() {pool.shutdown();}Testpublic void testObjectPool() {ExecutorService executor Executors.newFixedThreadPool(8);// execute 8 tasks in separate threadsexecutor.execute(new ExportingTask(pool, 1));executor.execute(new ExportingTask(pool, 2));executor.execute(new ExportingTask(pool, 3));executor.execute(new ExportingTask(pool, 4));executor.execute(new ExportingTask(pool, 5));executor.execute(new ExportingTask(pool, 6));executor.execute(new ExportingTask(pool, 7));executor.execute(new ExportingTask(pool, 8));executor.shutdown();try {executor.awaitTermination(30, TimeUnit.SECONDS);} catch (InterruptedException e) {e.printStackTrace();}} } 测试输出看起来像 Object with process no. 1 was created Object with process no. 2 was created Object with process no. 3 was created Object with process no. 4 was created Thread 2: Object with process no. 2 was borrowed Thread 1: Object with process no. 1 was borrowed Thread 2: Object with process no. 2 was returned Thread 3: Object with process no. 3 was borrowed Thread 4: Object with process no. 4 was borrowed Thread 1: Object with process no. 1 was returned Thread 4: Object with process no. 4 was returned Thread 8: Object with process no. 4 was borrowed Thread 5: Object with process no. 1 was borrowed Thread 7: Object with process no. 3 was borrowed Thread 3: Object with process no. 3 was returned Thread 6: Object with process no. 2 was borrowed Thread 7: Object with process no. 3 was returned Thread 5: Object with process no. 1 was returned Thread 8: Object with process no. 4 was returned Thread 6: Object with process no. 2 was returned 可以看出访问该池的第一个线程创建了驻留在池中的最少对象。 多次运行该测试类我们会发现有时4个对象相互借用并且会在池中创建一个新的5.对象。 所有测试类均可在GitHub中获得 。 参考来自JCG合作伙伴 Oleg Varaksin的简单轻量级池实现 位于“ 软件开发思想”博客上。 翻译自: https://www.javacodegeeks.com/2013/08/simple-and-lightweight-pool-implementation.htmljs 实现轻量ps
http://www.ihoyoo.com/news/47560.html

相关文章:

  • 网站换模板对seo网站建设哪个平台最好
  • 百度公司官方网站让人做网站 需要准备什么
  • 能够做简历的网站济南网站建设优化熊掌号
  • WordPress建站收费app的研发生产都包括什么
  • 长沙网站制作合作商python编程快速上手
  • 自己动手做网站如何将wordpress上传到站点
  • 中企中立做的网站好吗类似于wordpress
  • 佛山网站制作公司博爱seo排名优化培训
  • 商丘网站建设专业现状网站开发后期工作包括那两个部分
  • 新乡网站建设seo快速排名软件价格
  • 西安高端品牌网站建设网站推广app下载
  • 返利网站怎么做百度网站推广服务商
  • 哪个网站教做pptwordpress修改logo
  • 搜索网站的方法网站建设的合同条款
  • 湛江企业网站建设公司网络服务启动失败
  • 企业网站报备怎么找做网站的人
  • 顺义区快速建站免费地方门户网站源码
  • 临沂外贸国际网站建设wordpress 图片点击放大
  • 海口免费做网站建立长效机制
  • 广州网站seo地址一键开启网站
  • 网上商城网站建设意义还有哪些网站可以做淘宝活动吗
  • 网站建设实践考试试题网站留言模板
  • .gs域名做网站怎么样wordpress 搬家后无法打开
  • 专用车网站建设poedit pro wordpress
  • 怎么在网站中做视频背景wordpress多站
  • 网页上传 网站wordpress添加底部友情链接
  • 制作网站参考vs2012做网站
  • 电商网站制作公司安心保险官方网站
  • 济宁网站建设的公司汕头seo优化公司
  • 坪地网站建设怎么样wordpress 批量打印文章