|
>I think I made the assumption that Daniel is talking about >Servlets, where it makes sense to avoid synchronization and >not Swing. Well, in servlets, I am of the opinion that synchronization can be kept largely or entirely optional. The way I would work around the "multi-threading" of the servlet object architecture is that I create a "shadow" object (one-to-one with each servlet I need). At servlet entry, I construct the shadow object normally or perhaps fetch (using synchronize) from some stash of recently used shadow objects. I then initialize the shadow's instance variables with whatever I need. It would typically include stuff passed in, like the output and input objects, and so on. This is how I think the servlet architecture should have been defined to start with. I then do something like shadowobject.run() to do the real work of the servlet. Since the object that does the real work "sits" on the stack, it is inherently single-threaded (my code is started up in a single thread, no matter who else is logically capable of being launched in the underlying original Servlet object itself). It is different in detail, but it is functionally identical to implementing a run() method for a Runnable object. Perhaps someone can show that this is a bad idea, but it has worked for what I have done. This restores normal object-oriented rules at any rate -- I can treat instance variables in the shadow object normally and I'm back to normal considerations between the styles 1, 2, or 3 of my original note if I need more than the original thread to get work done. Now, if those "shadow" objects have to deal with state-ful beans of some kind, beans that live over the course of multiple servlet invocations, then I'll probably be synchronizing to reach those. Ditto for anything that for some reason has to sit in the servlet object. For the rest, I think I can be synchronize free and yet not have hard-to-think-about thread safety considerations. All this, as long as one can confine most or all of the work to the shadow object. What am I missing, here, that this would not usually work? Larry W. Loen - Senior Linux, Java, and iSeries Performance Analyst Dept HP4, Rochester MN
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.