<?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: Implementing a simple cache with Java</title>
	<atom:link href="http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Fri, 30 Jul 2010 04:19:58 +0200</lastBuildDate>
	
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: vikas</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-15719</link>
		<dc:creator>vikas</dc:creator>
		<pubDate>Mon, 17 May 2010 12:51:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-15719</guid>
		<description>Hi Christian,

Great and simple solution.</description>
		<content:encoded><![CDATA[<p>Hi Christian,</p>
<p>Great and simple solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Schenk</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-15257</link>
		<dc:creator>Christian Schenk</dc:creator>
		<pubDate>Tue, 27 Apr 2010 21:05:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-15257</guid>
		<description>Hi John,
that&#039;s exactly what I thought about.</description>
		<content:encoded><![CDATA[<p>Hi John,<br />
that&#8217;s exactly what I thought about.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-15253</link>
		<dc:creator>John</dc:creator>
		<pubDate>Tue, 27 Apr 2010 17:54:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-15253</guid>
		<description>Hi Christian,

Here&#039;s what I understood from you. Below is what I think should work now. Correct me if I&#039;m wrong.

&lt;pre lang=&quot;java&quot;&gt;
private Runnable createRemoveRunnable(final String name)
    {
        return new Runnable()
        {
            public void run()
            {
                synchronized (this)
                {
                    objects.remove(name);
                    expire.remove(name);
                }
            }
        };
    }
&lt;/pre&gt;
&#160;</description>
		<content:encoded><![CDATA[<p>Hi Christian,</p>
<p>Here&#8217;s what I understood from you. Below is what I think should work now. Correct me if I&#8217;m wrong.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Runnable</span> createRemoveRunnable<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    objects.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    expire.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Schenk</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-15212</link>
		<dc:creator>Christian Schenk</dc:creator>
		<pubDate>Sun, 25 Apr 2010 06:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-15212</guid>
		<description>Hi John,
I&#039;m pretty sure that adding synchronized to the run method of the thread won&#039;t cut it. Instead, you should sync on &lt;code&gt;this&lt;/code&gt; to make sure that both objects named &lt;code&gt;objects&lt;/code&gt; and &lt;code&gt;expire&lt;/code&gt; can&#039;t be modified during the expiration removal process.</description>
		<content:encoded><![CDATA[<p>Hi John,<br />
I&#8217;m pretty sure that adding synchronized to the run method of the thread won&#8217;t cut it. Instead, you should sync on <code>this</code> to make sure that both objects named <code>objects</code> and <code>expire</code> can&#8217;t be modified during the expiration removal process.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-15143</link>
		<dc:creator>John</dc:creator>
		<pubDate>Wed, 21 Apr 2010 20:51:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-15143</guid>
		<description>hi Christian,

How do you modify the code as Vladislav mentioned to prevent ConcurrentModificationAccessException from happening during the expiration removal process?

Will this work (adding synchronized keyword to the run method)?

public synchronized void run() {
  				for (final String name : expire.keySet()) {
...
...
  				}
}</description>
		<content:encoded><![CDATA[<p>hi Christian,</p>
<p>How do you modify the code as Vladislav mentioned to prevent ConcurrentModificationAccessException from happening during the expiration removal process?</p>
<p>Will this work (adding synchronized keyword to the run method)?</p>
<p>public synchronized void run() {<br />
  				for (final String name : expire.keySet()) {<br />
&#8230;<br />
&#8230;<br />
  				}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Schenk</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-11049</link>
		<dc:creator>Christian Schenk</dc:creator>
		<pubDate>Tue, 10 Nov 2009 18:35:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-11049</guid>
		<description>Hi Adriaan,
thanks for the hint - it&#039;s a great idea!</description>
		<content:encoded><![CDATA[<p>Hi Adriaan,<br />
thanks for the hint &#8211; it&#8217;s a great idea!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adriaan</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-11046</link>
		<dc:creator>Adriaan</dc:creator>
		<pubDate>Tue, 10 Nov 2009 16:18:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-11046</guid>
		<description>Hi Christian,

The approach I used to implement a cache like yours was to use java.util.LinkedHashmap. This offers functionality to set a limit to the size of the cache and remove the least recently used item from the cache when the limit is exceeded. Advantages are that you don&#039;t need a scheduled worker thread to clean up your cache and the maximum size of the cache can be pre-determined.</description>
		<content:encoded><![CDATA[<p>Hi Christian,</p>
<p>The approach I used to implement a cache like yours was to use java.util.LinkedHashmap. This offers functionality to set a limit to the size of the cache and remove the least recently used item from the cache when the limit is exceeded. Advantages are that you don&#8217;t need a scheduled worker thread to clean up your cache and the maximum size of the cache can be pre-determined.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Schenk</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-10720</link>
		<dc:creator>Christian Schenk</dc:creator>
		<pubDate>Sun, 25 Oct 2009 16:26:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-10720</guid>
		<description>Hi Bass,
we don&#039;t really need this extra thread because the get method makes sure that it doesn&#039;t return old data. But in case the get method isn&#039;t called and we keep adding objects to the cache over and over again, none of them will be removed which may result in an &lt;code&gt;OutOfMemoryError&lt;/code&gt; - to prevent this I added this housekeeping thread.</description>
		<content:encoded><![CDATA[<p>Hi Bass,<br />
we don&#8217;t really need this extra thread because the get method makes sure that it doesn&#8217;t return old data. But in case the get method isn&#8217;t called and we keep adding objects to the cache over and over again, none of them will be removed which may result in an <code>OutOfMemoryError</code> &#8211; to prevent this I added this housekeeping thread.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-10599</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Mon, 19 Oct 2009 14:26:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-10599</guid>
		<description>Great intro to caching. But can you explain why you need a separate thread each time the get method is called? It appears to be an on-demand rather than a housekeeping activity so why would you need a another thread just for that?</description>
		<content:encoded><![CDATA[<p>Great intro to caching. But can you explain why you need a separate thread each time the get method is called? It appears to be an on-demand rather than a housekeeping activity so why would you need a another thread just for that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sunil kumar</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/comment-page-1/#comment-8814</link>
		<dc:creator>sunil kumar</dc:creator>
		<pubDate>Sun, 12 Jul 2009 01:12:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comment-8814</guid>
		<description>Good stuff.
good to start with basic caching mechanism</description>
		<content:encoded><![CDATA[<p>Good stuff.<br />
good to start with basic caching mechanism</p>
]]></content:encoded>
	</item>
</channel>
</rss>
