[SNMP4J] InterruptedException not handled properly

Eugene R. Snider gene at cvtt.net
Thu Aug 7 05:39:53 CEST 2008


I put the catch for exception following the interrupt call instead of in 
the thread, you might try that.

Gene

        public void stop() {
                if ( agentThread != null && !agentThread.isInterrupted()) {
                        agentThread.interrupt();
                        try {
                                agentThread.join();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } finally {
                                agentThread = null;
                        }
                }
        }


Gene

Jonathan Louie wrote:
> I have a thread that does a SNMP get request periodically in a loop.
> Interrupting the thread should cause the loop to stop, but doesn't.
> Here's some pseudo-code:
>
> public class SnmpBug  {
> 		
> 	public void start() throws IOException {
> 		TransportMapping transport = new DefaultUdpTransportMapping();
> 		transport.listen();
> 		final Snmp snmp = new Snmp(transport);
>
> 			thread = new Thread() {
> 				@Override
> 				public void run() {
> 					try {
> 						while (!isInterrupted()) {
> 							ResponseEvent response = snmp.get(requestPdu, target);
> 							// handle response
>
> 							Thread.sleep(5000);
> 						}
> 					} catch (InterruptedException ie) {
> 						// interrupted, return
> 					}
> 					// this line is never reached
> 				}
> 			};
> 			thread.start();
> 		
> 	}
>
> 	public void stop() {
> 		if (thread != null) {
> 			thread.interrupt();
> 		}
> 	}
> }
>
> The bug is in Snmp.java line 820.  The library method could throw
> InterruptedException, or set interrupted to true.
>
> A good article on how to handle InterruptedException is here:
> http://www.ibm.com/developerworks/java/library/j-jtp05236.html
>
>
>   



More information about the SNMP4J mailing list