Threads and Interrupts: Continue or exit?

The official documentation and forum posts I could find are very vague on this. They say it's up to the programmer to decide whether to continue after being interrupted or exit, but I can't find any documentation of the conditions that would warrant one or the other.

Here is the code in question:

private final LinkedBlockingQueue<Message> messageQueue = new LinkedBlockingQueue<Message>();

// The sender argument is an enum describing who sent the message: the user, the app, or the person on the other end.
public void sendMessage(String address, String message, Sender sender) {
    messageQueue.offer(Message.create(address, message, sender));

private Thread senderThread;

private void startSenderThread(){

    if(senderThread == null || !senderThread.isAlive()){
        senderThread = new Thread(){
            public void run() {


private void loopSendMessage(){
    Message queuedMessage;

    // Should this condition simply be `true` instead?
        try {
            queuedMessage = messageQueue.poll(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            EasyLog.e(this, "SenderThread interrupted while polling.", e);
        if(queuedMessage != null)

// Queue in this context means storing the message in the database
// so it can be sent later.
private void sendOrQueueMessage(Message message){
    //Irrelevant code omitted.

The sendMessage() method can be called from any thread and at any time. It posts a new message to send to the message queue and starts the sender thread if it isn't running. The sender thread polls the queue with a timeout, and processes the messages. If there are no more messages in the queue, the thread exits.

It's for an Android app that automates SMS message handling. This is in a class that handles the outbound messages, deciding whether to immediately send them or save them to send later, as Android has an internal 100 message/hour limit that can only be changed by rooting and accessing the settings database.

Messages can be sent from different parts of the app simultaneously, by the user or the app itself. Deciding when to queue for later needs to be handled synchronously to avoid needing atomic message counting.

I want to handle interrupts gracefully, but I don't want to stop sending messages if there are more to send. The Java documentation on threading says most methods simply return after being interrupted, but that will leave unsent messages in the queue.

Could anyone please recommend a course of action?


I guess the answer depends on why you are being interrupted? Often threads are interrupted because some other process/thread is trying to cancel or kill it. In those cases, stopping is appropriate.

Perhaps when interrupted, you send out all remaining messages and don't accept new ones?

Need Your Help

Keep track of multiple alarms with Android

android alarmmanager android-alarms

I need to set a number of alarms that repeat weekly, to automatically put the phone on vibrate when the user is on some class (it's an academic app) and then reset the volume when the class finishe...

Is there a code-generator to create DataTable definition block from Excel Work sheet?

excel datatable unit-testing code-generation

Basically the thing I want to achieve is to have a data-table that I want to use in my unit tests. And when I run my unit tests, I do not want to read any excel file into a data-table -or any call to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.