"Programming Marathon" "021-database Connection Pool"

Source: Internet
Author: User

"Programming Marathon algorithm Directory" "021-Database Connection Pool" "Project Download >>>" 1 Topic Description

The web system often accesses the database frequently, and performance can be poor if each access creates a new connection. To improve performance, the architect decides to reuse the connection that has already been created. When a request is received and there are no remaining connections available in the connection pool, a new connection is created and is placed in the connection pool for subsequent requests when the request processing is complete. Now that you have a log of processing requests, please analyze how many connections the connection pool needs to create.

1.1 Input Description:

The input contains multiple sets of data, and the first row of each group of data contains a positive integer n (1≤n≤1000) that represents the number of requests. Immediately following n rows, each line contains a request ID ID (A, B, C 、......、 Z) and an operation (connect or disconnect).

1.2 Output Description:

For each set of data, the maximum number of connections to be created for the output connection pool.

1.3 Input Example:
6connectconnectconnectB disconnectC disconnect
1.4 Output Example:
2
2 ideas for solving problems

This is a simple question, see code comments.

3 Algorithm Implementation
Importjava.util.*;/** * Author: Wang Junshu * time:2016-05-13 09:57 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public  class Main {     Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt"));         while(Scanner.hasnext ()) {intn = scanner.nextint (); N *=2; list<string> request =NewArraylist<> (n); for(inti =0; I < n;            i++) {Request.add (Scanner.next ());        } System.out.println (maxconnection (request));    } scanner.close (); }/** * Maximum number of connections * * @param request operation, two groups (A, b), A for Connection name, table B operation content, connect or disconnect * @re Turn Maximum number of connections * /    Private Static int maxconnection(list<string> request) {//Record the maximum number of connections        intMax =0;//Save number of connectionsSet<string> conn =NewHashset<> (Request.size ()/2);//processing each connection request         for(inti =0, j = request.size (); I < J; i + =2) {String R = request.get (i); String o = request.get (i +1);//If the connection operation is added to the connection pool            if("Connect". Equals (o)) {Conn.add (R);The maximum value is updated when the size in the current connection pool is larger than the maximum value that was previously recorded                if(Conn.size () > Max)                {max = Conn.size (); }            }//Delete connection            Else if("Disconnect". Equals (o)) {conn.remove (R); }        }returnMax }}
4 Test Results

5 Other information

Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.

"Programming Marathon" "021-database Connection Pool"

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.