From 92869fa583418a750f2716b3e52c90c8843d3c8e Mon Sep 17 00:00:00 2001 From: Matt Tucker Date: Tue, 29 Mar 2005 19:54:29 +0000 Subject: [PATCH] Better threading logic when waiting for a connection (SMACK-37). git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2469 b35dd754-fafc-0310-a699-88a17e54d16e --- .../org/jivesoftware/smack/PacketReader.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/source/org/jivesoftware/smack/PacketReader.java b/source/org/jivesoftware/smack/PacketReader.java index 25948a5c0..afab76779 100644 --- a/source/org/jivesoftware/smack/PacketReader.java +++ b/source/org/jivesoftware/smack/PacketReader.java @@ -142,7 +142,21 @@ class PacketReader { // giving up and throwing an error. try { synchronized(connectionIDLock) { - connectionIDLock.wait(SmackConfiguration.getPacketReplyTimeout()); + if (connectionID == null) { + // A waiting thread may be woken up before the wait time or a notify + // (although this is a rare thing). Therefore, we continue waiting + // until either a connectionID has been set (and hence a notify was + // made) or the total wait time has elapsed. + long waitTime = SmackConfiguration.getPacketReplyTimeout(); + long start = System.currentTimeMillis(); + while (connectionID == null && !done) { + if (waitTime <= 0) { + break; + } + connectionIDLock.wait(waitTime); + waitTime -= System.currentTimeMillis() - start; + } + } } } catch (InterruptedException ie) { } @@ -252,8 +266,8 @@ class PacketReader { for (int i=0; i