make sure tag writer thread shuts down

This commit is contained in:
Daniel Gultsch 2017-04-22 10:08:51 +02:00
parent c93b1a86bb
commit c837e0616a
2 changed files with 10 additions and 9 deletions

View file

@ -13,15 +13,15 @@ import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
public class TagWriter {
private OutputStreamWriter outputStream;
private boolean finshed = false;
private boolean finished = false;
private LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>();
private Thread asyncStanzaWriter = new Thread() {
private boolean shouldStop = false;
@Override
public void run() {
while (!shouldStop) {
if ((finshed) && (writeQueue.size() == 0)) {
while (!isInterrupted()) {
if (finished && writeQueue.size() == 0) {
return;
}
try {
@ -29,7 +29,7 @@ public class TagWriter {
outputStream.write(output.toString());
outputStream.flush();
} catch (Exception e) {
shouldStop = true;
return;
}
}
}
@ -73,7 +73,7 @@ public class TagWriter {
}
public TagWriter writeStanzaAsync(AbstractStanza stanza) {
if (finshed) {
if (finished) {
Log.d(Config.LOGTAG,"attempting to write stanza to finished TagWriter");
return this;
} else {
@ -90,7 +90,7 @@ public class TagWriter {
}
public void finish() {
this.finshed = true;
this.finished = true;
}
public boolean finished() {
@ -102,7 +102,7 @@ public class TagWriter {
}
public synchronized void forceClose() {
finish();
asyncStanzaWriter.interrupt();
if (outputStream != null) {
try {
outputStream.close();

View file

@ -219,6 +219,7 @@ public class XmppConnection implements Runnable {
protected synchronized void changeStatus(final Account.State nextStatus) {
if (Thread.currentThread().isInterrupted()) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": not changing status to "+nextStatus+" because thread was interrupted");
return;
}
if (account.getStatus() != nextStatus) {
if ((nextStatus == Account.State.OFFLINE)
@ -454,7 +455,7 @@ public class XmppConnection implements Runnable {
* Starts xmpp protocol, call after connecting to socket
* @return true if server returns with valid xmpp, false otherwise
*/
private boolean startXmpp(Socket socket) throws Exception {
private synchronized boolean startXmpp(Socket socket) throws Exception {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}