From 6458351f6c03f839a384b56580ef89bcd8812e54 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 20 Jan 2018 23:13:36 +0100 Subject: [PATCH] use countdownlatch to check if TagWriter has finished --- .../eu/siacs/conversations/xml/TagWriter.java | 18 ++++++++++++++---- .../conversations/xmpp/XmppConnection.java | 12 +----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/xml/TagWriter.java b/src/main/java/eu/siacs/conversations/xml/TagWriter.java index bdd50aa34..df062b8cf 100644 --- a/src/main/java/eu/siacs/conversations/xml/TagWriter.java +++ b/src/main/java/eu/siacs/conversations/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 eu.siacs.conversations.Config; import eu.siacs.conversations.xmpp.stanzas.AbstractStanza; @@ -15,14 +17,16 @@ public class TagWriter { private OutputStreamWriter outputStream; private boolean finished = false; private LinkedBlockingQueue writeQueue = new LinkedBlockingQueue(); + 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(); @@ -31,10 +35,12 @@ public class TagWriter { outputStream.flush(); } } catch (Exception e) { - return; + break; } } + stanzaWriterCountDownLatch.countDown(); } + }; public TagWriter() { @@ -95,8 +101,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/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index a4facf5b7..2307b3a01 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1487,9 +1487,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) { @@ -1512,14 +1510,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; }