Object pool Mode

Source: Internet
Author: User

1. The object pool technology does not limit that only one object can be created, and this technology also applies to creating a fixed number of objects. However, in this case, you have to face the problem of sharing objects in the object pool.

When you create multiple objects at a high cost, you can consider using the object pool technology. The existing technologies include thread pool technology and database connection pool technology.

2. UML diagram (astah/Jude ):

 

3. simulate a database connection pool for implementation:

Implemented interface:

1 package COM. xinye. test. pool; 2/** 3 * all things the user needs implement this interface 4 * @ author Xinye 5*6 */7 public interface iconnection {8 object get (); 9 void set (Object OBJ); 10}

Implementation class:

1 package COM. xinye. test. pool; 2/** 3 * What the user really needs, for example, database connection 4 * @ author Xinye 5*6 */7 public class connection implements iconnection {8 9 @ override10 public object get () {11 // todo auto-generated method stub12 return NULL; 13} 14 15 @ override16 public void set (Object OBJ) {17 // todo auto-generated method stub18 19} 20 21}

Implementation class packaging object (add status ):

1 package COM. xinye. test. pool; 2/** 3 * items in the pool (items with status and what the user actually needs are actually package items) 4 * @ author Xinye 5*6 */7 public class poolitem {8 Public Boolean isuse; 9 Public iconnection conn; 10 public poolitem (iconnection conn) {11 this. conn = conn; 12} 13}

Pooled management objects:

1 package COM. xinye. test. pool; 2 3 Import Java. util. arraylist; 4/** 5 * Pool sub-management class 6 * @ author wangheng 7*8 */9 public class poolmanager {10 11 private arraylist <poolitem> items = new arraylist <poolitem> (); 12/** 13 * put things in the pool 14 * @ Param conn15 */16 public synchronized void add (iconnection conn) {17 items. add (New poolitem (conn); 18} 19/** 20 * Get the object 21 * @ return22 * @ throws poolemptyexception23 */24 public synchronized iconnection get () in the pool () throws poolemptyexception {25 int Len = items. size (); 26 for (INT I = 0; I <Len; I ++) {27 poolitem item = items. get (I); 28 If (item. isuse = false) {29 item. isuse = true; 30 return item. conn; 31} 32} 33 throw new poolemptyexception (); 34} 35/** 36 * release object 37 * @ Param conn38 * @ throws poolemptyexception39 */40 public synchronized void release (iconnection conn) throws poolemptyexception {41 int Len = items. size (); 42 for (INT I = 0; I <Len; I ++) {43 poolitem item = items. get (I); 44 If (conn = item. conn) {45 item. isuse = false; 46 return; 47} 48} 49 throw new poolemptyexception (); 50} 51/** 52 * The pool is empty. Exception 53 * @ author wangheng54 * 55 */56 public static class poolemptyexception extends exception {57/** 58*59 */60 private Static final long serialversionuid = 5617927009406425965l; 61 62} 63 64}

Connection Pool object:

1 package COM. xinye. test. pool; 2 3 Import COM. xinye. test. pool. poolmanager. poolemptyexception; 4 5/** 6 * users who really need to care about the pool 7 * @ author Xinye 8*9 */10 public class connectionpool {11 Private Static poolmanager manager = new poolmanager (); 12/** 13 * Add connection objects in batches 14 * @ Param count15 */16 public static void addconnections (INT count) {17 for (INT I = 0; I <count; I ++) {18 manager. add (New Connection (); 19} 20} 21/** 22 * Get the connection object 23 * @ return24 * @ throws poolemptyexception25 */26 public static iconnection getconnection () throws poolemptyexception {27 return manager. get (); 28} 29/** 30 * release link 31 * @ Param conn32 * @ throws poolemptyexception33 */34 public static void release (iconnection conn) throws poolemptyexception {35 manager. release (conn); 36} 37}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.