This article briefly introduces some tips used in the process of writing code to make JAVA code more efficient.
1. Put some system resources in the pool, such as database connections, threads, etc. In standalone's application, the database connection pool can be implemented using some open source connection pools, such as C3P0, proxool and DBCP, etc., and run in the container. Application 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.newFixedThreadPool(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 databases or remote services, try to merge multiple calls into one call.
3. Cache frequently accessed external resources into memory. You can simply use static hashmap to load when the application is started, or you can use some open source cache frameworks, such as OSCache and Ehcache. Synchronization with resources can be considered regularly Actively notify the query and external resource updates. Or leave the interface (command method or interface method) in the code you wrote yourself to manually synchronize.
4. Optimize IO operations. When JAVA operates files, it is divided into two categories: InputStream and OutputStream, Reader and Writer. The stream method should be fast. The latter is mainly used to operate characters. When the characters are only ASCII, you can use stream. Method to improve efficiency. Nio after JDK1.4 is better than io.
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:/temp/test.txt"))); out.write("abcde".getBytes()); out.flush() ; out.close() ;Use BufferedInputStream, BufferedOutputStream, BufferedReader, and BufferedWriter to reduce the number of direct accesses to disk.
FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); while (br.readLine() != null) count++;
5 Do not frequently use new objects. Use singleton mode for classes that only need one instance in the entire application. For String connection operations, use StringBuffer or StringBuilder. For utility-type classes, access them through static methods.
6. Avoid using wrong methods. For example, Exception can control the method of induction, but Exception must retain the performance of stacktrace consumption. Unless necessary, do not use instanceof for conditional judgment. Try to use the conditional judgment method of the ratio. Use highly efficient classes in JAVA, such as ArrayList Better than Vector.
7. Consideration of performance must be considered at the beginning of system analysis and design.
In short, the performance of a system when running is nothing more than considering optimization from three main aspects: CPU, Memory and IO. Reduce unnecessary CPU consumption, reduce unnecessary IO operations, and increase Memory utilization efficiency.