10/23/2004

Cool Runnables

A frequent reason to implement Runnable is, a class already extends other class we want it's objects to be active objects.

Another important reason is runnable can be used as a generic call back.
We can have a method like below doCallback where it calls back run on given runnable.

  doCallback(Runnable r){
    ...;
    r.run();
    ...
  }

This idiom is used in SwingUtilities.invokeLater(Runnable r), and SwingUtilitie.invokeAndWait(r). In these runnable is posted to a event queue and AWTEvent thread reads it and calls run from event thread. This makes given runnable called execute in a particular thread.

Recently I have been using this idiom in our persistence framework, we do normal operations on our business objects and post persistence tasks to a set of database aware threads. These db aware threads call our runnable task and do pre/post processing(transaction, connection management etc). Isn't it cool?

1 Comments:

At 2/04/2005, Blogger Srini said...

Couldn't get what situations will the business object needs to be isolated from db. For responsive system design it makes sense to delegate to the business layer, but why should business layer delegate it to 'db aware threads'. A related question could be what could happen when db operation fails?

 

Post a Comment

<< Home