Java Program Performance and Speed ​​Optimization Example

zhaozj2021-02-11  212

Java Program Performance and Speed ​​Optimization Example

David Lee

Example 1: Application I / O Buffer Function Class

Import java.io. *;

Public class Iotest {

Public static void main (string args []) {

Try {

FileReader fr = new fileReader (args [0]);

BufferedReader Br = New BufferedReader (FR);

While (br.readline ()! = null) {

System.out.println ("The File Content Are:" Br.Readline ());

}

fis.close ();

} catch (ioexception ie) {

System.out.Println ("The I / O Exception IS" IOE);

}

}

}

In the above example, the program uses a Class having a buffer function such that the read speed of Disk I / O is greatly improved. BufferedReader is a Java Class that replaces DataInputStream while increasing reading and writing speed. In the new Java version, DataInputStream is not recommended because its read and write is based on characters.

Example 2: String operation processing

Public class stringOperation {

Public static void main (string args []) {

String SqlQuery = NULL;

String sqlcondition = "conditionc = conditionD");

StringBuffer SB = new stringbuffer ();

Sb.append ("Select * from Database Table Where");

Sb.append ("Conditiona = ConditionB and");

IF (! sqlcondition.equals (null) {

sb.append (sqlcondition);

} else {

sb.append ("conditione = conditionf");

}

SqlQuery = sb.toString ();

// THEN Connect to the Database Ten Excute the Database Query

// .......

}

}

In the above example, use the StingBuffer Class to complete the database query, avoid using the " =" operation of String Class to reduce JVM to create new objects in memory, occupying resources, add JVM recycling resources burden. Readers can use Java Proflier feature to specifically compare different String operations, and JVM needs to complete how much resource recycling and runtime. Therefore, " =" directly " =" is very expensive in JVM.

Example 3: Handling expensive database initialization

At present, many websites can query the database through the web server, how to improve database query speeds into many programmers' attention. In Java Servlets or JSP, you can be implemented by init () or jspinit (), the following is an instance of a specific Java Servlet and database dialog.

Import java.io. *;

Import java.sql. *;

Import javax.servlet. *;

Import javax.servlet.http. *; public class databaseServlet Extends httpservlet {

Public void init (servletconfig conf) throws servletexception {

Super.init (conf);

Connection conn = NULL;

Try {

Class.Forname ("Sun.jdbc.odbc.jdcodbcdriver");

CONN = DriverManager.getConnection ("JDBC: ODBC: YourDSn,", "");

} catch (sqlexception sqle) {

System.err.Println ("Your Error Exception IS" SQLE);

} Catch (ClassNotFoundException CNFE) {

System.err.Println ("Your Error Exception IS" CNFE);

}

}

Public void doget (httpservletRequest Req, httpservletResponse res) throws

ServletException, IOException {

Res.SetContentType ("text / html");

ServletOutputStream out = null;

// Your HTML Formatter

Out.println ("Your HTML");

Try {

Statement Stmt = Conn.creatStatement ();

ResultSet RS = Stmt.excuteQuery ("SELECT * from YourDatabaseTable);

While (rs.next ()) {

// Processing your data

}

} catch (sqlexception sqle) {

Out.println ("The SQL Error IS" SQLE);

}

// Output your processing resulting resulting resulting resulting result to html page

Out.println ("Your HTML");

Rs.close ();

Stmt.close ();

}

Public void destroy () {

Try {

CONN.CLOSE ();

} catch (sqlexception sqle) {

System.err.Println ("Your SQL Error IS" SQLE);

}

}

}

In the above example, due to the characteristics of the Java Servlet running mechanism, only the INIT () of the INIT () in the entire servlet run is called to reduce unnecessary repetitive database operations. The reader can perform the database's Statement and the ResultSet section to complete the database's Statement and ResultSet section, or call PreparedStatement to optimize the calculation of the database in init (). At the same time, the closing of the connection to the database is done in a time of Destroy.

转载请注明原文地址:https://www.9cbs.com/read-4562.html

New Post(0)