This article briefly describes some of the techniques used in writing code to make Java code more efficient.
1, put some system resources in the pool, such as database connection, thread and so on. In standalone applications, database connection pools can be implemented using a number of open source connection pools, such as C3p0,proxool and DBCP, In applications running in containers this can use the datasource provided by the server. The thread pool can use the java.util.concurrent.ExecutorService provided by the JDK itself.
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ExecutorService;
public class Javathreadpool {public
static void Main (string[] args) {
Executorservice pool = Executors.newfixedt Hreadpool (2);
Thread T1 = new Mythread ();
Thread t2 = new Mythread ();
Thread t3 = new Mythread ();
Thread T4 = new Mythread ();
Thread T5 = new Mythread ();
Pool.execute (t1);
Pool.execute (T2);
Pool.execute (T3);
Pool.execute (T4);
Pool.shutdown ();
}
Class Mythread extends Thread {public
void run () {
System.out.println) (Thread.CurrentThread (). GetName () + " Running ... ");
}
2, reduce network overhead, when interacting with a database or remote service, try to combine multiple calls into a single call.
3, will frequently access the external resources cache into memory, simple can use static HashMap when the application started loading, you can also use some open source cache framework, such as Oscache and Ehcache. Synchronization with resources can take account of periodic polling and active notification of external resource updates. Or set aside an interface (either in the form of a command or an interface) in your own code to synchronize it manually.
4, optimize IO operations, Java operation files are divided into InputStream and Outputstream,reader and writer two classes, the stream is faster, the latter mainly for the operation of characters, You can increase the efficiency of the stream when the character is just ASCII. NIO is more efficient than IO after JDK1.4.
OutputStream out = new Bufferedoutputstream (New FileOutputStream ("D:/temp/test.txt"));
Out.write ("ABCDE". GetBytes ());
Out.flush ();
Out.close ();
Use Bufferedinputstream,bufferedoutputstream,bufferedreader,bufferedwriter to reduce the number of direct accesses to the disk.
FileReader FR = new FileReader (f);
BufferedReader br = new BufferedReader (FR);
while (Br.readline ()!= null) count++;
5 Do not frequent new objects, use singleton mode for classes that only have one instance in the entire application. For string concatenation operations, use StringBuffer or StringBuilder. For classes of type utility are accessed through static methods.
6, avoid the use of the wrong way, such as exception can control the introduction of the method, but exception to retain stacktrace consumption performance, unless necessary to use the instanceof do not judge the conditions, as far as possible to use the conditions of comparison to judge the way. Using the efficient classes in Java, For example, ArrayList is better than vector.
7, the consideration of performance should be considered at the beginning of system analysis and design.
In short, the performance of a system runtime is nothing more than the three main aspects of cpu,memory and IO to consider optimization. Reduce unnecessary CPU consumption, reduce unnecessary IO operation, increase memory utilization efficiency.