For all you Java experts: Synchronization w/Anonymous Inner Class?

95 pts.
Tags:
Anonymous inner class
Java
Java Synchronization
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

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following