diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-01-21 15:12:46 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-01-21 15:12:46 +0100 |
commit | 557a10664626975189cb05a2b5c139ca5b00d27c (patch) | |
tree | 10c4f190dc5bcd76e3c77d9ac2222dca2d6f72f9 /src/main/java/de/pixart/messenger | |
parent | ce02d3e2a9d6a5180ba458c97a4c25e09aba230c (diff) |
use countdownlatch to check if TagWriter has finished
Diffstat (limited to 'src/main/java/de/pixart/messenger')
-rw-r--r-- | src/main/java/de/pixart/messenger/xml/TagWriter.java | 17 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/xmpp/XmppConnection.java | 12 |
2 files changed, 14 insertions, 15 deletions
diff --git a/src/main/java/de/pixart/messenger/xml/TagWriter.java b/src/main/java/de/pixart/messenger/xml/TagWriter.java index c6b84170d..53f04128f 100644 --- a/src/main/java/de/pixart/messenger/xml/TagWriter.java +++ b/src/main/java/de/pixart/messenger/xml/TagWriter.java @@ -5,7 +5,9 @@ import android.util.Log; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import de.pixart.messenger.Config; import de.pixart.messenger.xmpp.stanzas.AbstractStanza; @@ -15,13 +17,15 @@ public class TagWriter { private OutputStreamWriter outputStream; private boolean finished = false; private LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>(); + private CountDownLatch stanzaWriterCountDownLatch = null; private Thread asyncStanzaWriter = new Thread() { @Override public void run() { + stanzaWriterCountDownLatch = new CountDownLatch(1); while (!isInterrupted()) { if (finished && writeQueue.size() == 0) { - return; + break; } try { AbstractStanza output = writeQueue.take(); @@ -30,9 +34,10 @@ public class TagWriter { outputStream.flush(); } } catch (Exception e) { - return; + break; } } + stanzaWriterCountDownLatch.countDown(); } }; @@ -94,8 +99,12 @@ public class TagWriter { this.finished = true; } - public boolean finished() { - return (this.writeQueue.size() == 0); + public boolean await(long timeout, TimeUnit timeunit) throws InterruptedException { + if (stanzaWriterCountDownLatch == null) { + return true; + } else { + return stanzaWriterCountDownLatch.await(timeout, timeunit); + } } public boolean isActive() { diff --git a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java index b242cd568..4ee5a6a4e 100644 --- a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java @@ -1517,9 +1517,7 @@ public class XmppConnection implements Runnable { final Socket currentSocket = this.socket; final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch; try { - for (int i = 0; i <= 10 && !currentTagWriter.finished() && !currentSocket.isClosed(); ++i) { - Thread.sleep(100); - } + currentTagWriter.await(1, TimeUnit.SECONDS); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": closing stream"); currentTagWriter.writeTag(Tag.end("stream:stream")); if (streamCountDownLatch != null) { @@ -1542,14 +1540,6 @@ public class XmppConnection implements Runnable { } } - private static void uninterruptedSleep(int time) { - try { - Thread.sleep(time); - } catch (InterruptedException e) { - //ignore - } - } - public void resetStreamId() { this.streamId = null; } |