A Standalone Approach to Threads

Veli Tasalı yazdı   ·   25 February 2019

experiment ideas

A scene from Where the Wild Things Are (2009) where Carol and Max stand next to each other

A scene from Where the Wild Things Are (2009) where Carol and Max stand next to each other

Running a second thread on Android is a tricky task. You may encounter crashes or funny looking GUI widgets when it is not managed correctly. We can stop it when the activity is recreated, but sometimes, we want it to complete in the background. If you are looking for this kind of scenario and want to reattach threads to the activities, this can help you do so.

@Override
protected void onDestroy() {
    super.onDestroy();
    getDefaultInterrupter().interrupt(false);
}

In the example above, getDefaultInterrupter() returns the class level instance of the Interrupter class notifying threads when they are interrupted. This is similar to Thread.interrupt(), but for more threads.

An Interrupter has two modes; user requests and any other request. A user request happens when the user (or something under the influence of the user) invokes the interrupter.

There is also the Closer class invoked by Interrupter. You can use this to release resources when the interrupter they belong to gets a signal for cancellation.

We will not need Interrupter for single task activities because they will only have one task to manage, similar to a music player having only one thread for showing the info for the playing song. However, deleting files or moving them is a type of task that can happen with multiple instances.

Activities that handle multiple tasks need a different approach. They need to own the threads in some way to have them back. We are going to use intents for that. Intents have different signatures and we can use this to refer to activities. You can take image viewers as an example. The activity showing an image will have the URI for it. We can show a text whenever the same intent data starts the activity. The simplest way could be to hash it. This example will return the same integer for the same intent:

public static int intentHash(@NonNull Intent intent)
{
    StringBuilder builder = new StringBuilder();

    builder.append(intent.getComponent());
    builder.append(intent.getData());
    builder.append(intent.getPackage());
    builder.append(intent.getAction());
    builder.append(intent.getFlags());
    builder.append(intent.getType());

    if (intent.getExtras() != null)
        builder.append(intent.getExtras().toString());

    return builder.toString().hashCode();
}

Now is time to attach and detach the activity. We will give threads nullable objects. This will ensure the safety of view-like elements outside of activities. We will also have a central thread manager which is a service in this case.

The thread manager will provide tasks with application context and resources (e.g. texts). It will also start and keep track of them.

Assume that we have a method similar to this:

public RunningTask findTaskByHash(int hashCode) {
    synchronized (mTaskList) {
       for (RunningTask runningTask : getTaskList())
           if (runningTask.hashCode() == hashCode)
               return runningTask;
    }
    return null;
}

This will return a task matching the hash code. We can access the task manager service by binding it to a context.

Dizi Understanding the Architecture of TrebleShot

Bu gönderi bir dizinin parçasıdır. Bu bölüm aynı dizideki diğer gönderileri gösterir.

TrebleShot Viki

A scene from Where the Wild Things Are (2009) where Carol and Max stand next to each other
okuyorsun en
A Standalone Approach to Threads

Veli Tasalı yazdı ·  25 February 2019

TrebleShot
en
A Guide to Developing TrebleShot Based Applications

Veli Tasalı yazdı ·  09 July 2019