You are currently browsing the monthly archive for May 2009.

When the main thread in a single-threaded application throws an uncaught exception, you are likely to notice because the stack trace is printed on the console (and because the program stops). But in a multithreaded application, especially one that runs as a server and is not attached to a console, thread death may be a less noticeable event, resulting in partial system failures that can cause confusing application behavior.

If we found a thread is dying due to an uncaught exception, the answer is simple: catch the exception at an appropriate place so that you can keep going. For example,

class SaferPoolWorker extends Thread {
    public void run() {
        IncomingResponse ir;

        while (true) {
            ir = (IncomingResponse) queue.getNext();
            PlugIn plugIn = findPlugIn(ir.getResponseId());
            if (plugIn != null) {
                try {
                    plugIn.handleMessage(ir.getResponse());
                }
                catch (RuntimeException e) {
                    // log the exception and move on
                }
            }
            else
                log("Unknown plug-in for response " + ir.getResponseId());
        }
    }
}

OK, the thread dies, and your object is gone? No! You said.
The robust multiple thread should Objects do not depend upon particular threads. If a thread should fail, the thread is recreated, and the object continues to run.”
Well, you know Thread can’t be restarted, so we need to recreate a Thread to load current object on it. Here is the example, The taming of the thread.

Reference
1. Hey, where’d my thread go?
2. Thread pools and work queues
3. Java while loop and Threads!
4. How to restart thread in java?
5. Java: How to stop a thread?

The Collections framework introduced iterators for traversing a list or other collection, which optimizes the process of iterating through the elements in a collection. However, the iterators implemented in the java.util Collections classes are fail-fast, which means that if one thread changes a collection while another thread is traversing it through an Iterator, the next Iterator.hasNext() or Iterator.next() call will throw ConcurrentModificationException.

Look at this,
[Only good for single thread]

   List list = new ArrayList();
   Iterator iterator = list.iterator();
   while(iterator.hasNext()) {
     Item item = iterator.next();
     if(isOld(item)) {
   	 iterator.remove();
     }
   }

if this is single-threaded code and need to modify a collection while iterating through it. That’s fine.
But you run in multiple-threaded, how to avoid ConcurrentModificationException?
First you may want to use synchronizedList like this,
[Scalability problems]

  List originalList = new ArrayList();
  List list = Collections.synchronizedList(originalList);
  synchronized(list) {
    Iterator iterator = list.iterator(); // Must be in synchronized block
    while (iterator.hasNext()) {
       Item item = iterator.next();
       if(isOld(item)) {
          iterator.remove();
       }
    }
  }

The bigger problem with the synchronized Collections wrappers(synchronizedList, synchronizedMap), and the earlier Hashtable and Vector classes, is that they synchronize on a single lock. This means that only one thread may access the collection at once, and if one thread is in the process of reading from a List/Map, all other threads that want to either read from it or write to it must wait.
So what’s perfect way to do?
[Concurrent Collections]

List originalList = new ArrayList();
List list = new CopyOnWriteArrayList(originalList);
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
     Item item = iterator.next();
     if(isOld(item)) {
       iterator.remove();
     }
}

The same story for Map, you should use ConcurrentHashMap instead synchronizedMap.

Reference:
1. Concurrent collections classes
2. Building a better HashMap
3. Java Concurrency Bugs #4: ConcurrentModificationException
4. Taming Tiger: Concurrent collections
5. Introduction to nonblocking algorithms
6. Concurrency: Blocking Queues and You

Update: ConcurrentHashMap example
1. Correct use of ConcurrentHashMap
2. Usage and performance of ConcurrentHashMap
3. Performance Optimization in ConcurrentHashMap

Update: ConcurrentSkipListSet example
1. Java Ordered Collections: Trees and Skip Lists
2. What is new in Java 6.0 Collections API?

Sometimes we frustrate for the business logic inside code. So we try to use many different design patterns to solve the problem. But we still experience like, Scattered Business Logic and mixed up with everything else, Business Logic hidden in Code, frameworks for everything else except business knowledge. I have spent some time to use JBoss jBPM to solve those problem, and it’s not perfect but it is better than before.

The basic Advantages to use jBPM is
1. Declarative Programming or Domain Specific Language (DSL)
2. Logic and Data Separation
3. Speed and Scalability
4. Centralization of Knowledge
5. Adopt JBoss Drool then you can set rule with your BPM.

Reference:
1. JBoss jBPM
2. JBoss jBPM articles
3. JBPM Tutorial-Hands On Guide
4. jBPM Tools Reference Guide
5. Top 10 Java Workflow Engine

Also my previous post discussed the web flow at front-end. Some projects consider the web flow pattern to combine with jBPM and make web page and business logic together such as Spring Web Flow and Seam Page Flow.
1. jBPM and Spring
2. Integrating jBPM3 With Spring
3. Seam and JBoss jBPM

At the end, you may be interested how to work with ESB…
1. JBoss jBPM meets ESB
2. ESB Service Orchestration with JBPM

You can use assertions to detect errors that may otherwise go unnoticed. Assertions contain Boolean expressions that define the correct state of your program at specific points in the program source code.
Reduce to use try/catch/finally to throw an exception instead of assertions.

Basic assertion helps you
1. Presents an overview of design by contract
2. Presents an overview of assertions
3. Shows how to roll your own assertion capabilities
4. Describes the new assertion facility
5. Shows how to use the new assertion facility
6. Offers guidelines for using assertions
7. Presents examples of how to use assertions

But you should watch the pitfall, too.

Reference:
1. Using Assertions in Java Technology
2. Programming With Assertions
3. Develop code using assertions