In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to increase the running efficiency of Java code". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to increase the running efficiency of Java code.
1, put some system resources in the pool, such as database connections, threads and so on. In standalone applications, database connection pooling can be implemented using some open source connection pooling, such as C3P0Gramproxool and DBCP, etc. In applications running in containers, DataSource. Thread pools can use the java.util.concurrent.ExecutorService provided by 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 and try to merge multiple calls into one call when interacting with a database or remote service.
3, cache the frequently accessed external resources into memory, simply use static's hashmap to load when the application starts, or you can use some open source cache frameworks, such as OSCache and Ehcache. Synchronization with resources can consider regular polling and proactive notification when external resources are updated. Or set aside an interface (command mode or interface mode) in the code you write to synchronize manually.
4, optimize IO operation, JAVA file operation is divided into InputStream and OutputStream,Reader and Writer two types, stream way is fast, the latter is mainly used to manipulate characters, when the character is only ASCII, you can use stream to improve efficiency. Nio after JDK 1.4 is more efficient 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,BufferedWriter to reduce the number of direct access to the disk.
FileReader fr = new FileReader (f); BufferedReader br = new BufferedReader (fr); while (br.readLine ()! = null) count++
Don't use frequent new objects, and use singleton patterns for classes that only need one instance in the whole application. For the connection operation of String, use StringBuffer or StringBuilder. Classes of type utility are accessed through static methods.
6. Avoid using wrong methods, such as Exception can control the introduction of methods, but Exception should retain the performance of stacktrace consumption, unless it is necessary not to use instanceof to make conditional judgment, try to use the conditional judgment method of ratio. Using efficient classes in JAVA, such as ArrayList, performs better than Vector.
7. the consideration of performance should be considered at the beginning of system analysis and design.
In a word, the performance of a system is optimized from three main aspects: CPU,Memory and IO. Reduce unnecessary CPU consumption, reduce unnecessary IO operations, and increase Memory utilization efficiency.
Thank you for your reading, the above is the content of "how to increase the running efficiency of Java code". After the study of this article, I believe you have a deeper understanding of how to increase the running efficiency of Java code. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.