<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Threadsafe Java Servlets</title>
	<atom:link href="http://mcherm.com/permalinks/1/threadsafe-java-servlets/feed" rel="self" type="application/rss+xml" />
	<link>http://mcherm.com/permalinks/1/threadsafe-java-servlets</link>
	<description>Adventures in Programming</description>
	<lastBuildDate>Fri, 02 Apr 2010 16:07:43 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Cthulhu</title>
		<link>http://mcherm.com/permalinks/1/threadsafe-java-servlets/comment-page-1#comment-20900</link>
		<dc:creator>Cthulhu</dc:creator>
		<pubDate>Tue, 09 Jun 2009 13:17:53 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=97#comment-20900</guid>
		<description>&quot;This can be avoided (somewhat awkwardly) by having a separate lock object for every possible variable and creating all of the lock objects at Session initialization.&quot;

For this problem, you could either declare those objects volatile, or, perhaps even better, make them Atomic. Check out the docs on atomic variables here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/package-summary.html

This&#039;ll allow you to read and write variables threadsafely, and also to compare and update them - the threading system may decide to switch to another thread in between a comparison and an update of a variable.</description>
		<content:encoded><![CDATA[<p>&#8220;This can be avoided (somewhat awkwardly) by having a separate lock object for every possible variable and creating all of the lock objects at Session initialization.&#8221;</p>
<p>For this problem, you could either declare those objects volatile, or, perhaps even better, make them Atomic. Check out the docs on atomic variables here: <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/package-summary.html" rel="nofollow">http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/package-summary.html</a></p>
<p>This&#8217;ll allow you to read and write variables threadsafely, and also to compare and update them &#8211; the threading system may decide to switch to another thread in between a comparison and an update of a variable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mcherm</title>
		<link>http://mcherm.com/permalinks/1/threadsafe-java-servlets/comment-page-1#comment-374</link>
		<dc:creator>mcherm</dc:creator>
		<pubDate>Tue, 30 Sep 2008 11:58:25 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=97#comment-374</guid>
		<description>When I searched around on the web for other descriptions of this problem and perhaps for people who have solutions (other than locking on the session) I found almost nothing that talked about it. It sometimes makes you wonder... am I imagining things? Is the problem real but everyone in the world is ignoring it, or is it that I am just confused about the issues.

Well, it turns out that I&#039;m not the only one. Joe Campbell pointed me toward a well-written article by article by Brian Goetz that describes this problem in detail. Brian is one of the world&#039;s top experts on Java threading -- among other things he is the author of &quot;Java Concurrency in Practice&quot;. So I am not confused... there IS a real issue here, and 99% of all web applications are broken.</description>
		<content:encoded><![CDATA[<p>When I searched around on the web for other descriptions of this problem and perhaps for people who have solutions (other than locking on the session) I found almost nothing that talked about it. It sometimes makes you wonder&#8230; am I imagining things? Is the problem real but everyone in the world is ignoring it, or is it that I am just confused about the issues.</p>
<p>Well, it turns out that I&#8217;m not the only one. Joe Campbell pointed me toward a well-written article by article by Brian Goetz that describes this problem in detail. Brian is one of the world&#8217;s top experts on Java threading &#8212; among other things he is the author of &#8220;Java Concurrency in Practice&#8221;. So I am not confused&#8230; there IS a real issue here, and 99% of all web applications are broken.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mcherm</title>
		<link>http://mcherm.com/permalinks/1/threadsafe-java-servlets/comment-page-1#comment-364</link>
		<dc:creator>mcherm</dc:creator>
		<pubDate>Wed, 24 Sep 2008 12:55:01 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=97#comment-364</guid>
		<description>Daniel:

Ah... apparently I misused the term &quot;copy on write&quot;. I was actually envisioning an immutable data structure held by reference in the (mutable) Session, just as you describe. This works great for threads that do reading (they can work with the copy that was valid when the thread began, even if subsequent updates occur). But I&#039;m still trying to figure out he details for threads that do writes. I don&#039;t quite understand your statement, &quot;Once you add simple Mutex locking to the shared mutable variable, then you’re good to go!&quot; but hopefully I&#039;ll figure it out.

I _do_ wonder why there aren&#039;t more published solutions out there for this problem.</description>
		<content:encoded><![CDATA[<p>Daniel:</p>
<p>Ah&#8230; apparently I misused the term &#8220;copy on write&#8221;. I was actually envisioning an immutable data structure held by reference in the (mutable) Session, just as you describe. This works great for threads that do reading (they can work with the copy that was valid when the thread began, even if subsequent updates occur). But I&#8217;m still trying to figure out he details for threads that do writes. I don&#8217;t quite understand your statement, &#8220;Once you add simple Mutex locking to the shared mutable variable, then you’re good to go!&#8221; but hopefully I&#8217;ll figure it out.</p>
<p>I _do_ wonder why there aren&#8217;t more published solutions out there for this problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mcherm</title>
		<link>http://mcherm.com/permalinks/1/threadsafe-java-servlets/comment-page-1#comment-363</link>
		<dc:creator>mcherm</dc:creator>
		<pubDate>Wed, 24 Sep 2008 12:46:02 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=97#comment-363</guid>
		<description>Joe:

I would love to hear about a servlet container that provides some kind of support *other* than the ability to process the requests one-at-a-time for a user (same as locking on the Session). I was under the impression that neither Tomcat, JBoss, WebSphere nor WebLogic offered that. Am I wrong about one of these, or do you know of another container which does?

As for the &quot;double object creation&quot; issue, I&#039;ll stop by your desk sometime; I&#039;d be interested to hear your ideas.</description>
		<content:encoded><![CDATA[<p>Joe:</p>
<p>I would love to hear about a servlet container that provides some kind of support *other* than the ability to process the requests one-at-a-time for a user (same as locking on the Session). I was under the impression that neither Tomcat, JBoss, WebSphere nor WebLogic offered that. Am I wrong about one of these, or do you know of another container which does?</p>
<p>As for the &#8220;double object creation&#8221; issue, I&#8217;ll stop by your desk sometime; I&#8217;d be interested to hear your ideas.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://mcherm.com/permalinks/1/threadsafe-java-servlets/comment-page-1#comment-362</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Wed, 24 Sep 2008 00:46:41 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=97#comment-362</guid>
		<description>Very nice analysis.  The biggest problem with concurrency is that people don&#039;t think about it sufficiently, leading to all those grand issues with mutable state, locking etc.

My personal favorite solution (for the moment) is the one undertaken by Clojure: immutable, persistent (*not* copy-on-write!) data structures referenced by shared mutable refs with concurrency semantics.  Unfortunately, to get the full impact of Clojure&#039;s references, you really need a transactional memory model, which isn&#039;t exactly something you can retrofit onto a language.  Still, &quot;altering&quot; an immutable data structure and storing the result in a shared mutable field is a lot better than actually modifying a single, shared mutable data structure.  Once you add simple Mutex locking to the shared mutable variable, then you&#039;re good to go!

As an aside, persistent data structures are a *lot* better than copy-on-write.  They&#039;re both fully-immutable, but copy-on-write is hideously inefficient for write operations since the entire structure must be copied.  Persistent data structures share structure between new and old versions.  With a fully-persistent data structure (like singly-linked lists), you don&#039;t need to copy anything on write, just make the new head point its tail to the old list.  More complicated data structures (like red-black trees) usually end up copying a little more, but almost never the entire structure.

Optimally, a fully-immutable data structure should preserve all of the performance guaranties of its mutable counterpart.  This is usually hard to achieve, but when it can be done the results are extremely compelling.</description>
		<content:encoded><![CDATA[<p>Very nice analysis.  The biggest problem with concurrency is that people don&#8217;t think about it sufficiently, leading to all those grand issues with mutable state, locking etc.</p>
<p>My personal favorite solution (for the moment) is the one undertaken by Clojure: immutable, persistent (*not* copy-on-write!) data structures referenced by shared mutable refs with concurrency semantics.  Unfortunately, to get the full impact of Clojure&#8217;s references, you really need a transactional memory model, which isn&#8217;t exactly something you can retrofit onto a language.  Still, &#8220;altering&#8221; an immutable data structure and storing the result in a shared mutable field is a lot better than actually modifying a single, shared mutable data structure.  Once you add simple Mutex locking to the shared mutable variable, then you&#8217;re good to go!</p>
<p>As an aside, persistent data structures are a *lot* better than copy-on-write.  They&#8217;re both fully-immutable, but copy-on-write is hideously inefficient for write operations since the entire structure must be copied.  Persistent data structures share structure between new and old versions.  With a fully-persistent data structure (like singly-linked lists), you don&#8217;t need to copy anything on write, just make the new head point its tail to the old list.  More complicated data structures (like red-black trees) usually end up copying a little more, but almost never the entire structure.</p>
<p>Optimally, a fully-immutable data structure should preserve all of the performance guaranties of its mutable counterpart.  This is usually hard to achieve, but when it can be done the results are extremely compelling.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Campbell</title>
		<link>http://mcherm.com/permalinks/1/threadsafe-java-servlets/comment-page-1#comment-361</link>
		<dc:creator>Joe Campbell</dc:creator>
		<pubDate>Wed, 24 Sep 2008 00:01:10 +0000</pubDate>
		<guid isPermaLink="false">http://mcherm.com/?p=97#comment-361</guid>
		<description>It should be noted the HttpSession is not inherently ThreadSafe or not - its just an interface that dictates what a servlet httpsession should provide not &#039;how&#039; it should provide it.

It is certainly possible to provide an implementation within your given container that supports the threading safety that you are looking for.  I would even venture to say that in some of the more popular servlet containers it is more then likely to already have been dealt with as an issue in the default implementation of the session.

There are also well known ways to deal with the &#039;double&#039; object creation issues that you outline.  I would be interested in talking to you about the problem you are attempting to solve so as to understand if you may be solving a problem that has already been dealt with.

Just my $0.02.
    Joe</description>
		<content:encoded><![CDATA[<p>It should be noted the HttpSession is not inherently ThreadSafe or not &#8211; its just an interface that dictates what a servlet httpsession should provide not &#8216;how&#8217; it should provide it.</p>
<p>It is certainly possible to provide an implementation within your given container that supports the threading safety that you are looking for.  I would even venture to say that in some of the more popular servlet containers it is more then likely to already have been dealt with as an issue in the default implementation of the session.</p>
<p>There are also well known ways to deal with the &#8216;double&#8217; object creation issues that you outline.  I would be interested in talking to you about the problem you are attempting to solve so as to understand if you may be solving a problem that has already been dealt with.</p>
<p>Just my $0.02.<br />
    Joe</p>
]]></content:encoded>
	</item>
</channel>
</rss>

