 




<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IT Answers &#187; Java Synchronization</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/tag/development/java-synchronization/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers</link>
	<description></description>
	<lastBuildDate>Sat, 25 May 2013 23:18:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>For all you Java experts: Synchronization w/Anonymous Inner Class?</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/for-all-you-java-experts-synchronization-wanonymous-inner-class/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/for-all-you-java-experts-synchronization-wanonymous-inner-class/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 19:27:32 +0000</pubDate>
		<dc:creator>Crabpot8</dc:creator>
				<category><![CDATA[Anonymous inner class]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Synchronization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Hey All, I have the following method, and I am unclear if the Timer being inside a synchronized method will cause the method to block for the duration of the Timer. private synchronized void _addToPendingHosts(final String hostURI) { if (pendingHosts_.contains(hostURI) == true) return; pendingHosts_.add(hostURI); // Schedule to check if we h ave received an ACK [...]]]></description>
				<content:encoded><![CDATA[<p>Hey All, </p>
<p>I have the following method, and I am unclear if the Timer being inside a synchronized method will cause the method to block for the duration of the Timer. </p>
<pre>
private synchronized void _addToPendingHosts(final String hostURI) {
		if (pendingHosts_.contains(hostURI) == true)
			return;
		
		pendingHosts_.add(hostURI);
		
		// Schedule to check if we h ave received an ACK from them
		// TODO - make sure 5 seconds is a decent time
		// TODO - create firewall notification
		
		Timer t = new Timer();
		t.schedule(new TimerTask() {
			public void run() {
				if (pendingHosts_.contains(hostURI))
					LogSender.sendErr("FIREWALL: from " + hostURI);
			}
			
		}, 5000);
	}
</pre>
<p>I have since re-factored the code to look like this, but the same question still stands</p>
<pre>
	private void _addToPendingHosts(CachedHost h) {
		synchronized (pendingHosts_) {
			if (pendingHosts_.containsKey(h.getURI()) == true)
				return;

			pendingHosts_.put(h.getURI(), h);
		}

		// Schedule to check if we have received an ACK from them
		// TODO - make sure 5 seconds is a decent time
		// TODO - create firewall notification

		final String hostURI = h.getURI();
		Timer t = new Timer("Pending Host - " + h.getURI());
		t.schedule(new TimerTask() {
			public void run() {
				// If they have not ACK'ed us yet, something is amiss
				synchronized (pendingHosts_) {
					if (pendingHosts_.containsKey(hostURI))
						LogSender.sendErr("FIREWALL: from " + hostURI);
				}
				// Kill this timer
				cancel();
			}

		}, 5000);
	}
</pre>
<p>Thanks!<br />
crabpot</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/for-all-you-java-experts-synchronization-wanonymous-inner-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 3/12 queries in 0.024 seconds using memcached
Object Caching 318/327 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-26 01:19:06 -->