User Tools

Site Tools



  • Don't use Vector or HashTable (which are synchronized) in single threaded programs. Use ArrayList or HashMap (unsynchronized) instead and avoid the overhead.
  • A recent study shows that java.nio can be faster than the old Slashdot Article


Taxonomy of how to achieve good concurrency:

  1. Immutable objects are inherently thread-safe
  2. Prefer high level abstractions: java.util.concurrent
  3. Or resort to low level locking with synchronized blocks and java.util.concurrent.locks
  4. Rarely use low level primitives: volatile variables or java.util.concurrent.atomic classes (This allows for non-blocking synchonization!)
  5. Never do deliberate undersynchronization

General hints

  • Format objects can not be shared amongst threads as they are not thread-safe!

Finding the right JAR

Frameworks like JBoss come with a great number of .jar Files on which your application can depend. Often you know which class you need but you dont know in which jar the class hides. To help you find the jar file you need to include in your build path change to the directory where you expect the .jar and use the command (replace ServiceMBeanSupport with the class you are looking for)

find . -name "*.jar" -exec bash -c "echo {} && jar tvf {} | grep ServiceMBeanSupport " \;
java_random_hints.txt · Last modified: 2012/08/26 17:20 by hkoller