Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

John Williamer

+ Follow
since Mar 27, 2002
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Williamer

Wouldn't caching the instance also lead to synchronization problems due to the fact that the cache will have to check for the existence of the object and if it doesn't exist then call the getInstance?
Thanks for the quick reply. I've read about DCL and it looks like I've got only two options (not including the ThreadLocal method).

Either sync the whole method or put the creation of the instance with the declaration.

The one problem I have found with sync-ing the whole method is that performance is hit (almost 3 times slower than a non synced method).

The other method of using private static final MyClass instance= new MyClass()
is error handling. Something about a classnotfound(static initializer) exception makes me un-easy.
I'd rather have the option to handle exceptions rather than throw a Runtime due to the way my object is created.

I guess I'm just wondering how people who need to lazy load get around the DCL problem without loosing performance and keeping their exception handling strategy?
Just a quick (or maybe not so quick question). On a recent bout of refactoring I came across a bunch of static initializers that would initialize some data and throw exceptions.
This was refactored to use lazy loading and data was now initialized in the constructor.

My question is what is the best way to synchronize the whole process of inisitalizing the instance of the class and make it as thread safe as possible.

What I came up with is:
Note: the syncObj is a static object used only to sync.

These are my reasons for the above code:

On line 2 it is more sensible to return the instance straight away without having to enter a synchronize block.

The synchronized block then checks if the instance is null just in-case two threads get to the label marked (a) at the same time (when c_instance is null).

I full accept that the chances of both threads hitting point (a) at the same time is negligible(or even impossible due to the fact that there is not actual code on line 3) but is this good practice?

So what do you guys think?
Well i passed 141 with 81%. Not bad considering I had to hold down a full time job, stealing time here and there to rivise. The exam itself was tricky in places and the first 30 minutes did panic me a little. Skipping quite a few questions. But after taking my time to review them everything was more clear.
For preperation I only used Professional XML 2nd Edition and whizlabs. The captures specified by javaranch whould almost definately inlude the Xpointer and XLink stuff and maybe even some of the XQuery as I did get some questions on that.
Reading some posts just before I took the exam did dent my confidence about whizlabs as some people really hate it. But to their credit it did help me alot with the scenarios in the exam plus if you are taking this exam then you should be able to notice the odd errors in the whizlabs exam. On the whole I think that it benefited me a great deal by trawling through previous posts(and i mean ALL OF THEM!!) and even looking at the experiences of the people who have taken the exam and have passed or failed.
One question I have is what next?
I have done the Java programmers and web compontent developer and now XML....
Thanks for all your help. G
Tim, have you taken your examination yet?
Is doEndTag invoked if doStartTag or doAfterBody don not complete successfully?
Value Objects reduce code duplication.
I disagree. I think that you should be getting at least a pass mark from taking the mocks for the first time. And then reviewing the questions in depth afterwards. I dont really see any point in measuring you ability to do the exam from additional taking of the same mocks over and over until you get 85% + . This only serves for you to learn the questions and not the principals behind them. Try only to take each exam once and learn from your mistakes so that if/when you take the same exam again you get a good mark.
i believe all objects are unBound before the session is destroyed. Hope that helps
Basically anything inside the web-inf cant be acessed directly, so no you cant put a jsp inside. Although you can get a file from it indirectly ie through getResourceAsStream from the ServletContext
This will map urls like http://localhost:8080/webapp_name/test/random/whaever

This just maps the /test/ so you have to use the last slash to access it
This is the same as above except that it must not have the last slash in the above example
hope this helps
Actually Mike Kumar, HttpServlet methods are protected but can be overiden as both public and protected. Just a small point.
your trying to set the value of the second question mark
I've just set up a JDBC realm that works fine it tomcat but then i linked to it through apache which works for the html, jsp etc but it doesnt let me login at all. When run directly from tomcat all is fine ie http://host:8080/webapp/index.html
but if i run it from apache ie
all i get is a failed login when i try. Any help would be greatfully recieved. Thanks
18 years ago
How would i go about login in to a restricted web page using jdbc realms through a servlet. I've got jdbc realms working fine but I'm looking to get the authentication from a servlet and passing it to tomcat. For example http://username assword@localhost:8080/restricted.html
This would work in the servlet with a redirect but the password is clear to see. Any help would be great.
18 years ago