-2

hi mate i have a simple thread that i want to terminate when flag interrupt is on. this is the code at the begin of run method

conn = false;
for (int i=0; (isInterrupted() == false) && (i < TRY_CONNECT) && !conn; i++) {
    try{
        Log.d(TAG,"TRY"+i);
        sock.connect();
        conn = true;
    } catch (IOException e) {
        try {
            sleep(5000);
        } catch (InterruptedException e1) {
            Log.d(TAG,"CATCH int");
            break;
        }
    }
}
if(isInterruped() == true)
    Log.d(TAG,"INT");

out the thread i call on him interrupt method, and it dont terminate the loop.. he dont see the interrupt that i call... how is possible ? For debug: out where i call interrupt i insert two print with log cat ... thread_reader.interrupt(); boolean b=thread_reader.isInterrupted(); Log.d(TAG,""+b); and on log cat the system print "false" how is possible ? i ve just call interrupt

esoni
  • 1,362
  • 4
  • 24
  • 37
  • How are we suppose to help you if we don't even know what programming language this is? – quantum Sep 08 '12 at 17:55
  • Looks a lot like Java to me, so I tagged it as such. – jefflunt Sep 08 '12 at 17:56
  • sorry for my english i explain better the concept... if i put break inside cath interrupt exception i solve the problem only if when i interrupt the thread , it is sleeping... but if itsnt sleeping ,my interrupt dont generate interrupt exception... the thread should see the flag in the header of loop... but nothing why ? – – esoni Sep 08 '12 at 19:10

2 Answers2

1

When you catch InterruptedException, simply break the loop. Do not rely on isInterrupted() check in the loop header, as the interrupted flag is cleared when the InterruptedException is thrown.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • the flag is cleared on InterruptedException? – esoni Sep 08 '12 at 18:09
  • yes if use break now its ok... but if when i call interrupt the thread is'nt sleep the loop dont see the interrupt why ? – esoni Sep 08 '12 at 18:15
  • (isInterrupted()==false) dont see my interrupt – esoni Sep 08 '12 at 18:16
  • sorry for my english i explain better the concept... if i put break inside cath interrupt exception i solve the problem only if when i interrupt the thread , it is sleeping... but if itsnt sleeping ,my interrupt dont generate interrupt exception... the thread should see the flag in the header of loop... but nothing why ? – esoni Sep 08 '12 at 18:45
  • when it is not sleeping, it checks isInterrupted(), which just returns interrupted status, and never throws exception. – Alexei Kaigorodov Sep 09 '12 at 06:04
  • ok but if is has the flag isInterrupted() should return true and loop should break but my loop continue , why ? – esoni Sep 09 '12 at 06:14
  • you call isInterrupted() just after sleep() finished without throwing InterruptedException, so the flag is not set. I am not sure if sock.connect() can throw InterruptedException because you did not tell what type of sock is. – Alexei Kaigorodov Sep 09 '12 at 07:50
  • BluetoothSocket of android , the flag must be set if not throw InterruptedException and i call interrupt.. – esoni Sep 09 '12 at 08:34
  • If connection succeed, you set conn = true, but do not break the loop, why? – Alexei Kaigorodov Sep 09 '12 at 09:13
  • ok but itsnt the problem, i test with a device where fail every try because the device is off – esoni Sep 09 '12 at 09:22
1

Whenever you catch InterruptedException this clears the interrupt flag on the thread. You need to do something like following pattern as a matter of course every time you do the catch:

try {
     sleep(5000);
} catch (InterruptedException e1) {
     Log.d(TAG,"CATCH int");
     // _always_ re-interrupt the thread since the interrupt flag is cleared
     Thread.currentThread().interrupt();
     // you probably want to break
     break;
}

As @Alexei mentioned, you could put a break or return in the catch block to exit the thread immediately. But either way, you should always re-interrupt the Thread so that other parts of your program can detect that an interrupt condition was set on the Thread.

See this question/answer for more information:

Why would you catch InterruptedException to call Thread.currentThread.interrupt()?

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
  • sorry for my english i explain better the concept... if i put break inside cath interrupt exception i solve the problem only if when i interrupt the thread , it is sleeping... but if itsnt sleeping ,my interrupt dont generate interrupt exception... the thread should see the flag in the header of loop... but nothing why ? – esoni Sep 08 '12 at 20:55
  • And you know this because if you put in a `break` and interrupt the thread, it doesn't always stop the loop @dario? Any chance that you are calling other code which has a `sleep(...)` or other statement that is catching `InterruptedException` and _not_ re-interrupting the thread? – Gray Sep 08 '12 at 21:57
  • if the thread is sleeping and i interrupt: system throw interupt exception and i catch it and break the loop and its all OK... but if when i interrupt the thread isn't sleeping there arent exception but the thread should have interrupt flag set to true but when i test it in the header of loop it is false... this is the problem ! please help me :) – esoni Sep 08 '12 at 23:19
  • out where i call interrupt i insert two print with log cat ... thread_reader.interrupt(); boolean b=thread_reader.isInterrupted(); Log.d(TAG,""+b); and on log cat the system print "false" how is possible ? i ve just call interrupt – esoni Sep 08 '12 at 23:34
  • It is possible if the thread caught `InterruptedException` and did not re-interrupt itself. Did you do a `Thread.currentThread().interrupt()` in the _catch_ block of the `InterruptedException`? – Gray Sep 09 '12 at 00:11
  • i debug and i try to press interrupt() when thread is'nt sleeeping and when the thread check it find the flag=false.. – esoni Sep 09 '12 at 06:26
  • if it isn't sleeping then what does it do? sock.connect()? Then it start sleeping after it and the flag triggers immediately when sleep is called, and then exception is thrown and flag is cleared. After that, isInterrupted() is invoked and sees the flag=false. So what do you expect instead? – Alexei Kaigorodov Sep 09 '12 at 10:56
  • suppose that when i call interrupt() the thread is executing connect(), the thread continue his execution and when return to the header of loop he should find the flag of interrupt on but the loop continue so he found the flag off... its a bug – esoni Sep 09 '12 at 12:45
  • sleep clear the interruption flag ? – esoni Sep 09 '12 at 12:45
  • i solve it ! sleep clear flag ! http://stackoverflow.com/questions/10401947/methods-that-clear-the-thread-interrupt-flag thanks for hep – esoni Sep 09 '12 at 12:48
  • No. That question is misleading @dario. I've edited it. `Thread.sleep()` clears the interrupt flag by immediately throwing `InterruptedException` either if the thread is interrupted while it is sleeping or if it has already been interrupted when sleep is called. – Gray Sep 09 '12 at 13:11
  • there arent solution... i think it is a bug of java – esoni Sep 09 '12 at 17:00
  • Sigh. You think wrong @Dario. There is no bug. It is working as designed/documented. – Gray Sep 10 '12 at 04:39