[concurrency-interest] RE: Concurrency-interest Digest, Vol 4, Issue 4

Unmesh joshi unmesh_joshi at hotmail.com
Wed May 4 20:07:12 EDT 2005


Thanks a lot. I like the second approach more. I am going to implement it 
immidiately.

>From: "David Holmes" <dholmes at dltech.com.au>
>To: "Unmesh joshi" 
><unmesh_joshi at hotmail.com>,<concurrency-interest at altair.cs.oswego.edu>
>Subject: RE: [concurrency-interest] RE: Concurrency-interest Digest, Vol 
>4,Issue 4
>Date: Thu, 5 May 2005 09:36:32 +1000
>
> > Thank you for all your replies. With all these,  I guess, I will be able
> > convince my tech lead.
> > I have a question though, about On Demand Holder Idiom. If the
> > constructor of Singleton is throwing exception, we will not be able to
> > use it?
> > public class Cache {
> >
> >
> >   private static class OnDemandHelher {
> >        static Cache instance = new Cache(); ///
> >   }
> >
> >   private Cache() throws InitializationException {
> >     //load data...
> >   }
> >
> > }
>
>Presuming that is a checked-exception, you would have to catch and convert
>to an unchecked exception:
>
>     private static class OnDemandHelper {
>         static Cache instance;
>
>         static {
>            try {
>                instance = new Cache();
>            }
>            catch (InitializationException e) {
>               throw (RuntimeException) new 
>RuntimeException().initCause(e);
>            }
>         }
>     }
>
>Or you could create your own subclass of RuntimeException to handle this
>directly. Of course you would have to adapt the handling of the exception 
>in
>the client code.
>
>You could also account for the exception in the helper another way:
>
>     private static class OnDemandHelper {
>         static Cache instance;
>	 InitializationException exception;
>
>         static {
>            try {
>                instance = new Cache();
>            }
>            catch (InitializationException e) {
>               exception = e;
>            }
>         }
>
>         static Cache instance() throws InitializationException {
>            if (instance != null)
>               return instance;
>            throw exception;
>         }
>     }
>
>This way client code still uses an instance() method that can throw, but
>that method is no longer responsible for initialization.
>
>Cheers,
>David Holmes
>

_________________________________________________________________
NRIs open a savings a/c with just INR 10,000 
http://creative.mediaturf.net/creatives/icicibank/TOL210405.htm FREE money 
transfers.



More information about the Concurrency-interest mailing list