Java Performance Tuning
Java(TM) - see bottom of page
Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks!
Training online: Concurrency, Threading, GC, Advanced Java and more ...
Tips April 2005
JProfiler
|
Get rid of your performance problems and memory leaks!
|
JProfiler
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 053 contents
http://www.javaspecialists.co.za/archive/Issue101b.html
Deadlock at classloading time (Page last updated January 2005, Added 2005-04-28, Author Heinz Kabutz, Publisher javaspecialists). Tips:
- It is possible to get a deadlock at classloading time by trying to acquire a lock on the class monitor in another thread while still in the static initializer, because the classloading process can have locked the class's monitor (e.g. by calling a synchronized static method as in the example provided).
http://today.java.net/pub/a/today/2005/04/19/desktoplive.html
Java Desktop Live Chapter 5: Swing Threading (Page last updated April 2005, Added 2005-04-28, Author Scott Delap, Publisher SourceBeat). Tips:
- The event dispatch thread handles two primary functions: it dispatches events, such as mouse clicks, to the appropriate Swing component; and it executes paint operations of Swing components.
- Calls in the actionPerformed() method execute in the event dispatch thread, and prevent any other paints executing while it runs.
- An AWTEventListener (added to the default toolkit) can slow your application down because it is executed on every AWT event.
- Freeze issues in most Swing applications occur when long running (or heavyweight) operations on the event dispatch thread are executed, which blocks the processing of other events.
- To avoid the possibility of deadlock, you must take extreme care that Swing components and models are created, modified, and queried only from the event-dispatching thread.
- Thread-safe Swing methods include repaint(), revalidate(), and invalidate() as well as adding and removing listeners.
- The methods, invokeLater() and invokeAndWait() form the foundation for allowing asynchronous operations in a Swing application.
- SwingUtilities.invokeLater(Runnable runnable) method posts its Runnable parameter to the event dispatch thread, and then immediately continues execution on the thread it was called from.
- For each addElement() method call, the listeners of a model are notified. These listeners execute on the thread that caused the notification, because models do not enforce execution on the event dispatch thread.
- Since CPU time is limited across all threads, CPU intensive tasks can conflict with drawing requirements. In such a case you might want to lower the priority of your worker threads so that the drawing thread gets priority.invokeAndWait() blocks execution of the thread invoking it until its Runnable finishes executing on the event dispatch thread.
- In most cases, you should use invokeLater() rather than invokeAndWait() since the calling thread is blocked until invokeAndWait() returns, which extends the duration of locks, increasing contention and chances of deadlock.
- Using invokeAndWait() allows you to gather input from the user on the event dispatch thread (EDT) and then continue processing on the calling non-EDT thread. invokeAndWait() guarantees that the Runnable will be processed before the calling thread continues.
- invokeAndWait() creates a lock object which is wait()'ed on in the calling thread while the Runnable is executed on the event dispatch thread. When the Runnable completes execution, the lock is notifyAll()'ed, so unblocking the calling thread.
- The recommended way to start your GUI in the main thread is via invokeLater(), e.g.
SwingUtilities.invokeLater(new Runnable() {public void run() {JFrame frame = new JFrame(); ...; frame.show();}});
- You can use SwingUtilities.isEventDispatchThread() to make methods in your code EDT thread safe by checking the thread they are being executed on and responding appropriately, e.g.
if (SwingUtilities.isEventDispatchThread()) { ... } else { SwingUtilities.invokeLater(new Runnable(){public void run() { ... } }); }
- javax.swing.Timer allows you to fire action events at a specified relative time value. Timer instances automatically post calls to their action listeners back onto the EDT using invokeLater() internally.
- All instances of javax.swing.Timer share one daemon thread called TimerQueue. This thread continuously waits until the next Timer scheduled to fire is ready.
- javax.swing.Timers have a coalesce setting, which you can use if you have a busy event display thread that cannot process Timer events as quickly as they are being posted. Instead of allowing the events to back up in the TimerQueue and continue to flood the thread, the backed up events will be coalesced and only one will be fired. A new firing time will then be scheduled.
- SwingWorker (https://jdnc.dev.java.net) is an abstract class that provides a structure for asynchronous threading in a thread safe manner.
- Swing uses an advanced technique in the implementation of Dialog.show(). If the thread executing show() is the event dispatch thread (EDT), it blocks the current EDT and starts a second EDT to process events while the current one is blocked.
- [Chapter also covers Foxtrot (http://foxtrot.sf.net) and Spin (http://spin.sf.net) synchronous threading solution for Swing.]
- [Chapter implements an automatic detector of Swing threading violations]
http://www.theserverside.com/articles/article.tss?l=ObjectCachingWithAOP
Object Caching With AOP (Page last updated September 2004, Added 2005-04-28, Author Srini Penchikala, Publisher TheServerSide). Tips:
- Object caching minimizes calls to back-end database, resulting in significant improvements in the application performance.
- Object caching offers fast access to data (advantage) but also suffers from memory overhead and synchronization complexity (disadvantages).
- Implementing a dynamically enabled caching capability allows a cache to be turned off or resized if it becomes a memory bottleneck.
- Caching logic should not provide any business functionality, it's not related to the domain of a business application.
- Cached data should be released from memory (purged), when the data is no longer needed, by using a pre-defined eviction policy.
- JBossCache's TreeCache can be used as a stand-alone cache (local) or a replicated cache (in a multi-server cluster environment); also, the cache replication can be done asynchronously or synchronously (synchronous means the client request is not returned until all cache changes are replicated in the cluster nodes).
- You should monitor cache usage to determine how effectively caching is being used (based on cache hit to miss ratio).
- Make sure you are only storing the expensive-to-access (and frequently requested data) in the cache.
http://www.javaworld.com/javaworld/jw-04-2005/jw-0418-train.html
The Train architecture for dynamically batching user requests (Page last updated April 2005, Added 2005-04-28, Author Edward Salatovka, Publisher JavaWorld). Tips:
- You should bear in mind that many database management systems cache data - which means that subsequent select statement against the same entry in the table would execute much faster.
- To make Tomcat work under heavy traffic, you need to modify the default Tomcat configuration file server.xml, e.g. to: maxProcessors="150" and acceptCount="150".
- You should use precompiled statements and connection pooling for database communications.
- The general technique that decreases the number of the trips to the database is to batch statements using Statement.addBatch() and Statement.executeBatch().
- A highly efficient SQL batching technique is to combine multiple SQL statements into one, e.g. "SELECT * FROM T where id=5" and "SELECT * FROM T where id=13" could be replaced with "SELECT * FROM T where id in (5,13)" fulfilling two or more user requests by one SQL statement inside one transaction scope.
- The fewer concurrent connections to the database you have, the fewer (depending on the isolation level) exclusive locks are acquired and fewer deadlocks and timeouts occur.
- If possible, combine statements into one SQL statement; where it's impossible to combine several SQL statements into one (for instance, a plain update statement) you should use statement.addBatch() to combine the statements by JDBC.
- The Train architecture groups jobs (intended db-communications) into batches; after a given time period or after a specific number of jobs are received in the batch, a worker generates SQL statements and interacts with the database; then delivers the results of the jobs to the corresponding requestor.
- Adaptive dynamic batching based on the predictive traffic analysis is where you change the maximum number of jobs in the batch depending on the number of user requests in the previous sessions - you can do this by taking a time window, measuring the number of requests in that window, then adjusting the batch size accordingly.
http://www-128.ibm.com/developerworks/java/library/wa-listload.html
How to load drop-down list values at application start-up time (Page last updated April 2005, Added 2005-04-28, Author Srinivasa Karanam, Publisher IBM). Tips:
- If you load common shared list values only once, you can reduce the number of database hits and make your Web application more efficient.
- Define the ApplicationServlet class to initiate the list values loading process at application start-up time.
- [Very specific article in one tiny aspect of the more generic performance tip "cache static data"].
Back to newsletter 053 contents
Last Updated: 2024-11-29
Copyright © 2000-2024 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
URL: http://www.JavaPerformanceTuning.com/news/newtips053.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us