João Vilaça João Vilaça - 1 year ago 94
Java Question

Java thread stuck after join

I have this Transmitter class, which contains one BufferedReader and one PrintWriter. The idea is, on the main class, to use Transmitter.receive() and Transmitter.transmit() to the main socket. The problem is:

public void receive() throws Exception {
// Reads from the socket
Thread listener = new Thread(new Runnable() {
public void run() {
String res;

try {
while((res = input.readLine()) != null) {
System.out.println("message received: " + res);

outputMessage = (res);

if (res.equals("\n")) {
} catch (IOException e) {


The thread changes the 'outputMessage' value, which I can get using an auxiliary method. The problem is that, without join, my client gets the outputMessage but I want to use it several times on my main class, like this:

while(trans1.getOutput() == null);
System.out.println("message: " + trans1.getOutput());

But with join this system.out never executes because trans1.receive() is stuck...any thoughts? help plz :/ If you need the rest of the code I can past here, thanks...

edit1: here is the transmitter class

Answer Source

You might send \n; that doesn't mean that you will see it in your Java code.

As it says in the Javadoc for BufferedReader.readLine() (emphasis mine):

(Returns) A String containing the contents of the line, not including any line-termination characters

so "\n" will never be returned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download