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

View File

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