diff --git a/src/kx/KConnection.java b/src/kx/KConnection.java index c01b2054..6dd3b376 100755 --- a/src/kx/KConnection.java +++ b/src/kx/KConnection.java @@ -14,7 +14,7 @@ public class KConnection { private final int port; private final String userPassword; private final boolean useTLS; - private boolean closed = true; + private volatile boolean closed = true; private DataInputStream inputStream; private OutputStream outputStream; @@ -29,7 +29,11 @@ void io(Socket s) throws IOException { } public void close() { - // this will force k() to break out i hope + if (socketReader != null) { + socketReader.interrupt(); + socketReader = null; + } + if (closed) return; closed = true; @@ -79,9 +83,9 @@ private void connect() throws IOException, K4AccessException { closed = false; socketReader = new SocketReader(s); - Thread socketReaderThread = new Thread(socketReader, "Reader " + host + ":" + port); - socketReaderThread.setDaemon(true); - socketReaderThread.start(); + socketReader.setName("Reader " + host + ":" + port); + socketReader.setDaemon(true); + socketReader.start(); } public KConnection(String h, int p, String userPassword, boolean useTLS) { @@ -122,9 +126,9 @@ public K.KBase k(K.KBase x) throws K4Exception, IOException, InterruptedExceptio return k(x, null); } - private class SocketReader implements Runnable { + private class SocketReader extends Thread { - private DataInputStream inputStream; + private final DataInputStream inputStream; private KMessage message = null; private ProgressCallback progress = null;