Make Java code more efficient

  • 2020-04-01 04:10:58
  • OfStack

This article gives you a brief overview of some of the techniques used to make JAVA code more efficient when writing code.

1, put some system resources in the pool, such as database connections, thread, etc. In the standalone applications, database connection pool can use some open source connection pool implementation, such as C3P0 proxool and DBCP, running in the application of the container that can use server provides a DataSource. The thread pool can use the JDK itself provide Java. Util. Concurrent. The ExecutorService.


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 and try to merge multiple calls into one call when interacting with a database or remote service.

3, will often visit the external resources to the cache memory, simple application can use the static hashmap in startup load, also can use some open source framework cache, such as OSCache and Ehcache. And resources synchronization can consider regular polling and external resource update when active notice. Or in writing your own code to set aside the interface (command mode or interface mode) for a manual synchronization.

4, optimize IO operation, when JAVA operation files are divided into InputStream and OutputStream,Reader and Writer, the way of stream is faster, the latter is mainly for the operation of characters, when characters are only ASCII can be used to improve the efficiency of the way of stream.


OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:/temp/test.txt")));       
out.write("abcde".getBytes());    
out.flush(); 
out.close(); 

Using the BufferedInputStream, BufferedOutputStream BufferedReader, BufferedWriter reduce the number of direct access to the disk.


FileReader fr = new FileReader(f); 
BufferedReader br = new BufferedReader(fr); 
while (br.readLine() != null) count++; 

For String join operations, use StringBuffer or StringBuilder. For utility type classes, use static methods.

6, avoid using the wrong way, such as Exception can control the method to be pushed out, but Exception should keep stacktrace to consume performance, do not use instanceof to make conditional judgment unless it is necessary, try to use more conditional judgment method.

7. Performance considerations should be considered at the beginning of system analysis and design.

In short, a system's runtime performance is optimized in terms of CPU, Memory, and IO, reducing unnecessary CPU consumption, reducing unnecessary IO operations, and increasing Memory efficiency.


Related articles: