"Java" "Tij" lets the number of objects in a class be controlled by US--the limit of the number of class objects implemented (perfect version) __java

Source: Internet
Author: User


In the previous article, I introduced to you a control of the number of objects to implement the way (see link: http://blog.csdn.net/wolfofsiberian/article/details/39856367) but given that This program is not complete enough to achieve. Because, we can think of, in the actual use process, the connection object in the process that the user uses, when the user first applies to, then releases the connection. The release process is a random one, for example, when the connected object is released Connarray[3], at which point the class should recycle the connarray[3, and if the user releases the Connarray[4 connection, the class also needs to recycle it. To assign them to other users when they apply.


Problem Description :

Create a class named ConnectionManager that manages a fixed array of elements that are connection objects. Client programmers cannot create connection objects directly, but only by using a static method in ConnectionManager to get them. When an object is no longer in the ConnectionManager, it returns a null reference. Detect these classes in main (). (here ConnectionManager is replaced with the following connectionmanagerenhence)

The concrete realization idea is as follows:

<pre name= "code" class= "java" >/** * Thinking in Java Test 6.8 (Limit class object count) Perfect version * @author: Wolfofsiberian * @email: Quietwol
F@yeah.net * * Import Java.sql.DriverManager;
Import java.sql.Connection;


Import java.sql.SQLException;
	public class connectionmanagerenhence{//maximum number of connections private static final int connmax = 10;
	Connection object static array private static Connection connarray[] = new Connection[connmax];
	
	The number of connections currently in use private static int countuse = 0; /** * Construction Method Privatization/Private Connectionmanagerenhence () {//}/** * Gets the subscript * of an array of available connection objects * @return Return can create a new connect
	The subscript index of the static array of the Ion object, or 1 if no subscript index is available.
			* * private static int getavailable () {for (int i=0;i<connmax;i++) {if (null = = Connarray[i]) {return i;
	}} return-1; /** * Get Connection Object * @return return the newly created Connection object * * public static Connection getconnection () throws exception{I

			F (countuse >= Connmax) {System.out.println ("Sorry,there is no Connection Object for your To use!");
		return null;
}		else {class.forname ("Com.mysql.jdbc.Driver"). Newinstance ()//load driver int connecting to MySQL database Availab
		        Leindex = getavailable ()//Get available Connection object array subscript//system.out.print ("+availableindex+"); Connarray[availableindex] = drivermanager.getconnection ("Jdbc:mysql://localhost/test?") + "user=root&password=sine88")//Get Connection object connection countuse++;//Use the number of connections plus one, the number of available connections is less than one System.out.println ("

		    	Assigning 1 Connection objects: index= "+availableindex+" Current online connections: "+countuse+", current number of connections available: "+ (Connmax-countuse));"
		return Connarray[availableindex]; }/** * Frees the specified Connection object * @param conn connection required to be freed/public static void Freeconnection (Connection conn) throws
					exception{for (int i=0;i<connmax;i++) {if (conn!= null) {if (connarray[i] = = conn) {connarray[i].close ();
					Connarray[i] = null; countuse--;//frees a Connection object System.out.println ("Free 1 Connection objects: index=" +i+ "Current online connections:" +countuse+ ", current number of connections available:" + (
				Connmax-countuse));
	}
			}	}//main portal public static void main (string[] args) throws exception{Connection conn[]=new connection[11];
			for (int i=0;i<11;i++) {Conn[i] = connectionmanagerenhence.getconnection ();
		System.out.println (conn[i]+ ":" +connarray[i]);
		for (int j=0;j<10;j=j+2) {connectionmanagerenhence.freeconnection (conn[j]);	
	} conn[2] = Connectionmanagerenhence.getconnection (); }
}


Run result :

F:\01 java\01 project\01 Tinkinginjava>java connectionmanagerenhence
Assigning 1 Connection objects: index=0 current online connections: 1, current available connections: 9
Assigning 1 Connection objects: Index=1 current online connections: 2, current available connections: 8
Assigning 1 Connection objects: index=2 Current online connections: 3, current available connections: 7
Assigning 1 Connection objects: index=3 current online connections: 4, current available connections: 6
Assigning 1 Connection objects: index=4 current online connections: 5, current available connections: 5
Assigning 1 Connection objects: index=5 current online connections: 6, Current available connections: 4
Assigning 1 Connection objects: index=6 Current online connections: 7, Current available connections: 3
Assigning 1 Connection objects: index=7 Current online connections: 8, current available connections: 2
Assigning 1 Connection objects: Index=8 current online connections: 9, current available connections: 1
Assigning 1 Connection objects: index=9 current online connections: 10, current available connections: 0
Sorry,there is no Connection Object for your to use!
Release 1 Connection objects: index=0 current online connections: 9, current available connections: 1
Release 1 Connection objects: index=2 Current online connections: 8, current available connections: 2
Release 1 Connection objects: index=4 Current online connections: 7, Current available connections: 3
Release 1 Connection objects: index=6 current online connections: 6, Current available connections: 4
Release 1 Connection objects: Index=8 current online connections: 5, current available connections: 5
Assigning 1 Connection objects: index=0 current online connections: 6, Current available connections: 4


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.