Austin B
Austin B

Reputation: 1600

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));
    startSenderThread();
}

private Thread senderThread;

private void startSenderThread(){

    if(senderThread == null || !senderThread.isAlive()){
        senderThread = new Thread(){
            @Override
            public void run() {
                loopSendMessage();
            }
        };

        senderThread.start();
    }
}

private void loopSendMessage(){
    Message queuedMessage;

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

// 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?

Upvotes: 0

Views: 321

Answers (1)

dkatzel
dkatzel

Reputation: 31648

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?

Upvotes: 1

Related Questions