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

360网站做不了网银怎么办杭州网站商场开发

360网站做不了网银怎么办,杭州网站商场开发,网页制作专业选择,宁波模板做网站对象池是包含指定数量的对象的容器。 从池中获取对象时#xff0c;在将对象放回之前#xff0c;该对象在池中不可用。 池中的对象具有生命周期#xff1a;创建#xff0c;验证#xff0c;销毁等。池有助于更好地管理可用资源。 有许多使用示例。 特别是在应用程序服务器中… 对象池是包含指定数量的对象的容器。 从池中获取对象时在将对象放回之前该对象在池中不可用。 池中的对象具有生命周期创建验证销毁等。池有助于更好地管理可用资源。 有许多使用示例。 特别是在应用程序服务器中有数据源池线程池等。在以下情况下应使用池 高频使用相同的物体 对象很大消耗很多内存 对象需要很多时间进行初始化 对象使用大量的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.html
http://www.yutouwan.com/news/174913/

相关文章:

  • 企业如何实现高端网站建设服务器上装wordpress
  • 网站备案由别人代网站仿制教程
  • 个人网站如何做淘宝客猪八戒网可以做网站吗
  • 盐城微信公众平台网站制作wordpress访问许可
  • 网站建设官网多少钱做国际网站怎么能快速打开
  • 网站xml地图建立网站坐等访问者发现
  • 网站建站那个好wordpress登录页面图标修改
  • 单页网站与传统网站的区别建设网站 深圳
  • 门户网站建设背景网站开发 手机 电脑
  • 汽车展示网站活动推广方式
  • 网站建设技术可行性分析梁头网站建设
  • 北京制作手机网站读取别人网站代码自己做
  • 成品网站 售卖商城平台推广方案
  • 仿腾讯网站源码网站建设宣传语怎么写
  • 音乐建设网站沈阳装修公司网站建设
  • 湖北网站seo策划昌吉北京网站建设
  • 忻府网站建设排名做网站开发有前途吗
  • 网站后台生成文章很慢上海公司注册官网
  • 用asp做网站需要准备什么机械设备上海网站建设
  • 做网站的软件word免费做网站怎么做网站
  • 网站刷流量对网站有影响吗教学ppt模板免费下载完整版
  • 保定比较好的网站建设公司计算机网络技术出来干什么
  • 东莞高端网站建设多少钱凡科网站备案
  • 有哪些做推送的网站网络运维主要做什么
  • 宿迁做网站吴江建网站
  • 佛山新网站制作咨询免费做网站怎么做网站链接
  • 万户网络技术有限公司网站优化过度被k
  • 专业制作外贸网站的公司深圳网站建设公司是
  • 北京网站托管wordpress+万能搜索页
  • 干网站建设销售怎么样个人网站作品下载