国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

基本對象池的使用

2019-11-18 12:07:00
字體:
供稿:網(wǎng)友

  /**
  * <p>Title: 基本對象池(org.apache.commons.pool.impl.GenericObjectPool)的使用 </p>
  * <p>Description:
  * 測試 org.apache.commons.pool.impl.GenericObjectPool 和 org.apache.commons.pool.PoolableObjectFactory的使用.</p>
  * <p>基本對象池的使用,
  * <LI>class TestGenericObjectPool 表示一個測試使用對象池的具體例子
  * <LI>class WdzPoolableObjectFactory 表示1個自己定義的生成對象的工廠
  * <LI>class Student 表示 需要使用對象池來維護(hù)的類</p>
  * <p>
  * 引用了 common-collcetions-2.1 ,commons-pool-1.1
  * </p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: netsky</p>
  * @author wdz( hotmail =wdz123@hotmail.com)
  * @version 1.0
  * @see {@link TestGenericObjectPool},
  * org.apache.commons.pool.impl.GenericObjectPool,org.apache.commons.pool.PoolableObjectFactory
  */
  
  public class Student {
  PRivate String sex;
  private String name;
  private int studentid;
  private int age;
  public Student() {
  }
  
  public static void main(String[] args) {
  Student student1 = new Student();
  
  }
  
  public String getSex() {
  return sex;
  }
  
  public void setSex(String sex) {
  this.sex = sex;
  }
  
  public String getName() {
  return name;
  }
  
  public void setName(String name) {
  this.name = name;
  }
  
  public int getStudentid() {
  return studentid;
  }
  
  public void setStudentid(int studentid) {
  this.studentid = studentid;
  }
  
  public void clear() {
  studentid = 0;
  name = "";
  age = 0;
  sex = "";
  
  }
  
  public int getAge() {
  return age;
  }
  
  public void setAge(int age) {
  this.age = age;
  }
  
  public String toString() {
  return "id =" + studentid + ",name =" + name + ",age=" + age + ",sex=" +
  sex;
  }
  }
  
  Class WdzPoolableObjectFactory --code
  package impl_genericobjectpool_test;
  
  import org.apache.commons.pool.impl.*;
  import org.apache.commons.pool.*;
  
  /**
  * <p>Title: 基本對象池(org.apache.commons.pool.impl.GenericObjectPool)的使用 </p>
  * <p>Description:
  * 測試 org.apache.commons.pool.impl.GenericObjectPool 和 org.apache.commons.pool.PoolableObjectFactory的使用.</p>
  * <p>基本對象池的使用,
  * <LI>class TestGenericObjectPool 表示一個測試使用對象池的具體例子
  * <LI>class WdzPoolableObjectFactory 表示1個自己定義的生成對象的工廠
  * <LI>class Student 表示 需要使用對象池來維護(hù)的類</p>
  * <p>
  * 引用了 common-collcetions-2.1 ,commons-pool-1.1
  * </p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: netsky</p>
  * @author wdz( hotmail =wdz123@hotmail.com)
  * @version 1.0
  */
  
  
  public class WdzPoolableObjectFactory
  implements PoolableObjectFactory {
  
  /**
  * 創(chuàng)建對象實例。同時可以分配這個對象適用的資源。
  * **/
  public Object makeObject() throws Exception {
  return new Student();
  };
  
  /**
  * 銷毀對象,實際上提供了釋放對象占用資源的接口。
  * destroyObject is invoked on every instance when it is being "dropped"
  * from the pool (whether due to the response from validateObject,
  * or for reasons specific to the pool implementation.)
  * */
  public void destroyObject(Object obj) throws Exception {
  }
  
  /***
  * 這個方法一般在 activateObject 方法執(zhí)行后調(diào)用
  * 檢查對象的有效性
  * validateObject is invoked in an implementation-specific fashion to
  * determine if an instance is still valid to be returned by the pool.
  * It will only be invoked on an "activated" instance.
  * **/
  public boolean validateObject(Object obj) {
  return true;
  }
  
  /**
  * 激活一個對象。
  * activateObject is invoked on every instance before it is returned from the pool.
  * **/
  public void activateObject(Object obj) throws Exception {
  }
  
  /**
  * 掛起一個對象
  * passivateObject is invoked on every instance when it is returned to the pool
  * **/
  public void passivateObject(Object obj) throws Exception {
  if (obj instanceof Student) {
  ( (Student) obj).clear();
  }
  }
  }
  package impl_genericobjectpool_test;
  
  import org.apache.commons.pool.impl.*;
  import org.apache.commons.pool.*;
  
  /**
  * <p>Title: 基本對象池(org.apache.commons.pool.impl.GenericObjectPool)的使用 </p>
  * <p>Description:
  * 測試 org.apache.commons.pool.impl.GenericObjectPool 和 org.apache.commons.pool.PoolableObjectFactory的使用.</p>
  * <p>基本對象池的使用,
  * <LI>class TestGenericObjectPool 表示一個測試使用對象池的具體例子
  * <LI>class WdzPoolableObjectFactory 表示1個自己定義的生成對象的工廠
  * <LI>class Student 表示 需要使用對象池來維護(hù)的類</p>
  * <p>
  * 引用了 common-collcetions-2.1 ,commons-pool-1.1
  * </p>
  * <p>Copyright: Copyright (c) 2004</p>
  * <p>Company: netsky</p>
  * @author wdz( hotmail =wdz123@hotmail.com)
  * @version 1.0
  */
  
  public class TestGenericObjectPool {
  private void testBorrowObject(GenericObjectPool pool, int kk) {
  try {
  Student[] student = new Student[10];
  for (int i = 0; i < 10; i++) {
  student[i] = (Student) (pool.borrowObject());
  student[i].setStudentid(i * 10000 + kk);
  student[i].setAge(i * 1000 + kk);
  student[i].setName("wdzname" + i * 100 + kk);
  }
  for (int i = 0; i < 10; i++) {
  System.out.println("student[" + i + "]=" + student[i]);
  pool.returnObject(student[i]);
  if (i == 9) {
  //察看對象回到對象池的狀態(tài)
  System.out.println("****student[9]=" + student[9]);
  }
  }
  
  }
  catch (Exception ex) {
  ex.printStackTrace();
  }
  
  }
  
  public TestGenericObjectPool() {
  org.apache.commons.pool.PoolableObjectFactory factory = new
  WdzPoolableObjectFactory();
  GenericObjectPool pool = new GenericObjectPool(factory, 4000 * 10,
  GenericObjectPool.WHEN_EXHAUSTED_GROW, 3000 * 10, true, true);
  System.out.println("group1");
  testBorrowObject(pool, 1);
  System.out.println("group2");
  testBorrowObject(pool, 2);
  
  }
  
  public static void main(String[] args) {
  TestGenericObjectPool test = new TestGenericObjectPool();
  }
  }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邯郸县| 潞西市| 常德市| 南陵县| 永新县| 高唐县| 乳山市| 信宜市| 手机| 崇左市| 邢台市| 贡觉县| 阿勒泰市| 临夏县| 岐山县| 宕昌县| 江孜县| 龙川县| 建湖县| 喀什市| 肇庆市| 礼泉县| 定兴县| 昌乐县| 绵竹市| 汉沽区| 云霄县| 钟山县| 玛纳斯县| 新闻| 正定县| 辛集市| 泸州市| 佳木斯市| 星子县| 深圳市| 东台市| 贵溪市| 东台市| 芜湖市| 象山县|