0

My problem is simple. I have several threads, but one gets interrupted at random. Who interrupted it?

How do I find out where in my application (or perhaps in a library) my thread gets interrupted?

There is no explicit interrupt() anywhere in the program.

[edit] I can't share much code because it's a large proprietary system and calls jump all over the place. I think most of it can be summarized here (out of my head):

public void run() {
    while (true) {
        sendReceive();
        try{ Thread.sleep(500); } catch (InterruptedException ex) {}
    }
}

public void sendReceive() {
    synchronized(stringBuilder) {
        out.write("data to send\n");
        startReceiving();
        stringBuilder.wait();
        System.out.println(stringBuilder.toString());
    }
}

public void startReceiving() {
    new Thread(new Runnable() {
        public void run() {
            synchronized (stringBuilder) {
                String s;
                while ((s = in.readLine()) != null) { stringBuilder.append(s); }
                stringBuilder.notifyAll();
            }
        }
    }).start();
}

It gets interrupted in stringBuilder.wait(); causing a popup saying communication error. In the communication log I see two events of the data being sent, and only one data received, like this:

Sent: "Get status\n"
Received: "1231"
Sent: "Get status\n"
Received: "632"
Sent: "Get status\n" <-- interrupt happens after this, but thread continues.
Sent: "Get status\n"
Received: "3568903"
Sent: "Get status\n"
Received: "6"
...

I'm not using any thread pooling.

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50
  • 2
    Threads are prone to spurious wakeups and interrupts in the Java threading model. You will have show us your code if you want help. – Boris the Spider Jul 10 '14 at 14:01
  • I think the OP is actually asking how to determine X, though I agree code would be helpful nonetheless. How are you determining it was interrupted? – Christopher Wirt Jul 10 '14 at 14:01
  • Are you using `ExecutorService`? Or some other thread manager? That might be calling `interrupt()` on your thread. – Jamie Cockburn Jul 10 '14 at 14:09
  • ... and posting actual stack trace can, at least, show which method was interrupted -- it can be a useful hint. – Victor Sorokin Jul 10 '14 at 14:12
  • 1
    @BoristheSpider threads are prone to spurious interrupts? Can you give me an example scenario of that? I've not seen a single inexplicable thread interrupt in my life. – MK. Jul 10 '14 at 14:16
  • @MK This is good SO question about it: http://stackoverflow.com/questions/1050592/do-spurious-wakeups-actually-happen – Victor Sorokin Jul 10 '14 at 14:20
  • 1
    it's "spurious wakeup", not "spurious interrupt". i don't think the interrupt flag gets set. @Mark, please show us some code so we have something to go on. otherwise this looks too vague to be answerable. – Nathan Hughes Jul 10 '14 at 14:32
  • Its probably that you are shutting down your thread pool. Please share code. You can find good explanation here http://stackoverflow.com/questions/2126997/who-is-calling-the-java-thread-interrupt-method-if-im-not – veritas Jul 10 '14 at 18:41
  • Spurious wakeups cannot cause interrupts, but they definitely can cause a `wait()` call to return prematurely. Always call `wait()` in a loop that is checking the condition on which you're waiting. See the [documentation](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--). – VGR Jul 12 '14 at 14:53

0 Answers0