openxc / openxc-android

Android library for accessing vehicle data from an OpenXC vehicle interface
BSD 3-Clause "New" or "Revised" License
236 stars 115 forks source link

Handle Interrupted Exceptions #351

Closed PPRAKA30 closed 4 years ago

PPRAKA30 commented 4 years ago

Changes

Notes

By searching online, found out that a thread should handle the exception in this way as to set it's interrupt flag. This is just to let high level methods know that the thread may terminate and allow the higher level to handle it if needed. As a general rule, we shouldn't just swallow.

This is done to keep state. When you catch the InterruptException and swallow it, you essentially prevent any higher level methods/thread groups from noticing the interrupt. Which may cause problems. By calling Thread.currentThread().interrupt(), you set the interrupt flag of the thread, so higher level interrupt handlers will notice it and can handle it appropriately.

Only code that implements a thread's interruption policy may swallow an interruption request. General-purpose task and library code should never swallow interruption requests.

Reference