0

I have a Thread (let's say T1) which reads data from socket:

public void run() {
  while (running) {
    try {
      BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream()) ); 
      String input = reader.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}

Another Thread (lets say T2) try to finish the program in one of its method. Therefore T2 does the following:

T1.running = false;
socket.close();

Here is this scenario for which i couldn't find a solution:

  • T1 is active and waiting for some input to read i.e. blocking.
  • context switching
  • T2 is active and sets running to false, closes the socket
  • context switching
  • because T1 was blocking and T2 closed the socket, T1 throws an Exception. What i want is to catch this SocketException. i can't put a try/catch(SocketException) in T1.run(). So how can i catch it in T1's running-method? If it's not possible to catch it in T1's running, then how can i catch it elsewhere?

PS: "Another question about the Thread Debugging"
Normally when i debug the code step by step, i lose the 'active running line' on a context switch. Let's say i'm in line 20 of T1, context switch happens, let's assume the program continues from the 30.line of T2, but the debugger does not go/show to the 30.line of T2, instead the 'active running line' vanishes. So i lose the control over the code. I use Eclipse for Java and Visual Studio for C#. So what is the best way to track the code while debugging on a context switch ?

5
  • 1
    SocketException extends IOException. So your catch clause should catch SocketException. Commented Nov 28, 2012 at 5:05
  • Why can't you catch SocketException in T1's run() method? (It's a subclass of IOException, so it will be caught anyway by the existing catch clause. If you want to catch it separately, you just have to list it in an earlier catch clause.) Commented Nov 28, 2012 at 5:05
  • I know that. But somehow, i don't know why, the program doesn't catch it. I mean i put a breakpoint there, but the program never stops in the catch block. Commented Nov 28, 2012 at 5:09
  • Why are you instantiating the reader inside the loop? Commented Nov 28, 2012 at 5:10
  • @Blesh: actually i do it elsewhere. i just put it there to shorten the code and focus on readline Commented Nov 28, 2012 at 5:13

2 Answers 2

1

For your problem assuming you are using a Thread Pool maybe you should make a ThreadFactory that installs a Thread.UncaughtExceptionHandler on all Threads and then invoke your work with execute() on the ExecutorService instead of submit().

For you problem with debugging maybe you should read http://msdn.microsoft.com/en-us/library/ms164746.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

Your code has several other problems so I'll address them all at the same time.

  1. You must create the BufferedReader outside the loop. Otherwise you will lose data in the buffers being discarded each time around the loop.
  2. You must test the result of readLine() for null. If you get it, you must close the BufferedReader and exit the loop.
  3. If you get any exception you must also close the BufferedReader and exit the loop.

What i want is to catch this SocketException.

So catch it.

I can't put a try/catch(SocketException) in T1.run().

You must. No choice. You have to completely rewrite it anyway because of the above items.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.