95 pts.
 For all you Java experts: Synchronization w/Anonymous Inner Class?
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 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);
	}
I have since re-factored the code to look like this, but the same question still stands
	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);
	}
Thanks! crabpot

Software/Hardware used:
ASKED: April 13, 2009  7:27 PM
UPDATED: April 13, 2009  7:49 PM

Answer Wiki:
Last Wiki Answer Submitted:  Be the first to answer this question.
All Answer Wiki Contributors:  Be the first to answer this question.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _