use countdownlatch to check if TagWriter has finished

This commit is contained in:
Daniel Gultsch 2018-01-20 23:13:36 +01:00
parent 3a8855a672
commit 6458351f6c
2 changed files with 15 additions and 15 deletions

View file

@ -5,7 +5,9 @@ import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.xmpp.stanzas.AbstractStanza; import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
@ -15,14 +17,16 @@ public class TagWriter {
private OutputStreamWriter outputStream; private OutputStreamWriter outputStream;
private boolean finished = false; private boolean finished = false;
private LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>(); private LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>();
private CountDownLatch stanzaWriterCountDownLatch = null;
private Thread asyncStanzaWriter = new Thread() { private Thread asyncStanzaWriter = new Thread() {
@Override @Override
public void run() { public void run() {
stanzaWriterCountDownLatch = new CountDownLatch(1);
while (!isInterrupted()) { while (!isInterrupted()) {
if (finished && writeQueue.size() == 0) { if (finished && writeQueue.size() == 0) {
return; break;
} }
try { try {
AbstractStanza output = writeQueue.take(); AbstractStanza output = writeQueue.take();
@ -31,10 +35,12 @@ public class TagWriter {
outputStream.flush(); outputStream.flush();
} }
} catch (Exception e) { } catch (Exception e) {
return; break;
} }
} }
stanzaWriterCountDownLatch.countDown();
} }
}; };
public TagWriter() { public TagWriter() {
@ -95,8 +101,12 @@ public class TagWriter {
this.finished = true; this.finished = true;
} }
public boolean finished() { public boolean await(long timeout, TimeUnit timeunit) throws InterruptedException {
return (this.writeQueue.size() == 0); if (stanzaWriterCountDownLatch == null) {
return true;
} else {
return stanzaWriterCountDownLatch.await(timeout, timeunit);
}
} }
public boolean isActive() { public boolean isActive() {

View file

@ -1487,9 +1487,7 @@ public class XmppConnection implements Runnable {
final Socket currentSocket = this.socket; final Socket currentSocket = this.socket;
final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch; final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch;
try { try {
for (int i = 0; i <= 10 && !currentTagWriter.finished() && !currentSocket.isClosed(); ++i) { currentTagWriter.await(1,TimeUnit.SECONDS);
Thread.sleep(100);
}
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": closing stream"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": closing stream");
currentTagWriter.writeTag(Tag.end("stream:stream")); currentTagWriter.writeTag(Tag.end("stream:stream"));
if (streamCountDownLatch != null) { 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() { public void resetStreamId() {
this.streamId = null; this.streamId = null;
} }