Java program performance and speed optimization instance

Source: Internet
Author: User

Example 1: The application has the 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 ());
}
FCM. Close ();
}
Catch (ioexception IOE)
{
System. Out. println ("the I/O exception is" + IOE );
}
}
}
   
In the above example, the program uses a class with the buffer function, which greatly improves the read Speed of disk I/O. Bufferedreader is a Java class that replaces datainputstream and improves read/write speed. In the new Java version, datainputstream is not recommended because its read and write operations are based on characters.
   
Example 2: String operation
   
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 then excute the database query
//.......
}
}
   
In the above example, stingbuffer class is used to create a database query, avoiding the "+ =" Operation of string class to reduce JVM creation of objects in memory and occupying resources, this increases the JVM Resource Recycling burden. You can use the Java proflier function to compare different string operations. The JVM needs to collect resources and run time. Therefore, it is very expensive to directly perform "+ =" on strings in JVM.

Example 3: process expensive database Initialization

At present, many websites can query databases through Web servers. How to improve the database query speed has become a concern of many programmers. Java servlets or JSP can be implemented through Init () or jspinit (). Below is a specific Java Servlet and database dialog instance.
    
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 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 operating mechanism, the expensive database initialization operation is completed in the init () called only once during the entire servlet operation, to reduce unnecessary repetitive database operations. The reader can move the statement and resultset parts of the database to Init () based on the actual situation of the application, or call preparedstatement and callablestatement to optimize database operations. At the same time, the closure of the database connection is completed by destroy () at one time.

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.