Java schema Object Pool Summary code example

Source: Internet
Author: User

Package Org.rui.util;import java.util.arraylist;/** * Object Multiplexing mode *  * @author PC * */public class poolmanager{//Connection pool object Publi C Static class Poolitem{boolean InUse = False;object item;//pool data Poolitem (Object item) {This.item = Item;}}    Connection Pool Collection Private ArrayList items = new ArrayList ();p ublic void Add (Object item) {This.items.add (new Poolitem (item));} Static class Emptypoolexception extends Exception{}public Object get () throws emptypoolexception{for (int i = 0; i < it Ems.size (); i++) {Poolitem Pitem = (poolitem) items.get (i); if (Pitem.inuse = = False) {Pitem.inuse = True;return Pitem.item;}} throw new Emptypoolexception ();//return null;} /** * Release Connection * @param item */public void release (Object item) {for (int i = 0; i < items.size (); i++) {Poolitem Pitem = (Po Olitem) Items.get (i); if (item = = Pitem.item) {pitem.inuse = False;return;}} throw new RuntimeException (item + "NOT NULL");}}


Package Org.rui.util;import org.junit.test;/** * Object Pool * * There is no restriction that only one object can be created. The same technique applies to creating a fixed number of objects, but * Yes, in which case you have to face the problem of how to share objects in the object pool. If the shared object is a problem, you might consider a check-in (check-in) checkout (check-out) of the shared object as a solution * scenario. For example, in the case of a database, a business database typically limits the number of connections that can be used at a given time. The following example implements the management of these database connections with the object pool. First, the basic management of the object pool object (a pool of objects) is implemented as a separate class. * * @author PC * */interface connection{object get (); void set (Object x);} Class Connectionimplementation implements Connection{public Object get () {return null;} public void set (object s) {}}class connectionpool{//Pool management Object private static Poolmanager pool = new Poolmanager ();//Specify number of connections and add PU Blic static void addconnections (int number) {for (int i = 0; i < number; i++) Pool.add (New Connectionimplementation ());} Get connection public static Connection getconnection () throws Poolmanager.emptypoolexception{return (Connection) pool.get (); Releases the specified connection public static void Releaseconnection (Connection c) {pool.release (c);}} Demo using public class Connectionpooldemo {static{connectionpool.addconnections (5);} @Testpublic void Test () {Connection c = null;try{//Gets the connection c = Connectionpool.getconnection ();} catch ( Poolmanager.emptypoolexception e) {throw new RuntimeException (e);} Set Value C.set (new Object ());//Get C.get ();//Release connectionpool.releaseconnection (c);} @Testpublic void Test2 () {Connection c = null;try{c = Connectionpool.getconnection ();} catch ( Poolmanager.emptypoolexception e) {throw new RuntimeException (e);} C.set (New Object ()); C.get (); Connectionpool.releaseconnection (c);} public static void Main (String args[]) {//Junit.textui.TestRunner.run (Connectionpooldemo.class);}}


Java schema Object Pool Summary code example

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.