This article provides a brief introduction to some of the techniques used to make JAVA code more efficient in writing code .
1. put some system resources in the pool (such as database connections , threads, etc.)
In standalone applications , database connection pools can be implemented using a number of open source connection pools , such as c3p0, Proxool, and DBCP wait , applications running in containers can use the server-provided DataSource. The thread pool can use JDK itself is provided by the Java.util.concurrent.ExecutorService.
1 Importjava.util.concurrent.Executors; 2 3 ImportJava.util.concurrent.ExecutorService; 4 5 Public classJavathreadpool {6 7 Public Static voidMain (string[] args) {8 9Executorservice pool = Executors.newfixedthreadpool (2); Ten One A -Thread T1 =NewMyThread (); - theThread t2 =NewMyThread (); - -Thread t3 =NewMyThread (); - +Thread T4 =NewMyThread (); - +Thread T5 =NewMyThread (); A at - - Pool.execute (t1); - - Pool.execute (T2); - in Pool.execute (T3); - to Pool.execute (T4); + - the * Pool.shutdown (); $ Panax Notoginseng } - the}
1 class extends Thread { 23public void run () { 4 5 System.out.println (Thread.CurrentThread (). GetName () + "running ...."); 6 7 } 89 }
2. Reduce network overhead (when interacting with a database or remote service , try to merge multiple calls into a single call)
3. Cache the frequently accessed external resources in memory
Simple can use static hashmap to load when the app starts , or you can use some open source cache Framework , such as Oscache and Ehcache . Synchronization with resources can take into account periodic polling and external resource updates when proactive notification; Or in your own code to leave out the interface ( command mode or interface mode ) manual Synchronization .
4. Optimize io action
java When you manipulate a file, you divide inputstream and outputstream, reader and writer two types of stream The way is fast the latter is mainly used to manipulate characters in characters is just ascii stream The way to improve efficiency
after JDK1.4, nio is more efficient than io .
OutputStream out = new Bufferedoutputstream (new FileOutputStream (New File ("D:/test.txt"));
Out.write ("ABCD". GetBytes ());
Out.flush ();
Out.close ();
use bufferedinputstream, bufferedoutputstream, BufferedReader, bufferedwriter to reduce the number of direct disk accesses .
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 need to have one instance throughout the application .
6. For a String connection Operation , use stringbuffer or StringBuilder.
7. Classes for utility types are accessed through static methods .
Avoid using the wrong way , Exception can control the method launch , but Exception to retain stacktrace Consumption performance , unless necessary, do not use instanceof to make conditional judgments , try to use the ratio of criteria to determine the way .
8. Use High-efficiency classes in JAVA , such as ArrayList better than Vector Performance .
9. Performance considerations 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 operations , and increase Memory Utilization Efficiency .
Get Java code to run faster