Stitch — Threading using Annotations

Working on a project, you often need to write the same code again and again. Same thing had happened to me. When I was working on a project, I faced a problem with threading. I had to create a thread object, then pass a runnable or callable to it. It becomes more tedious when you want method to return something, you have to create a FutureTask and you have to repeat yourself.

To deal with it, I found RxJava (API for asynchronous programming and the best so far) but it is very hard to understand it and use it properly. However I suggest you to use it in any project if you want asynchronous programming. So I thought to create a library that generates the boilerplate code for threading.

Stitch — What is it

In the most simple words, Stitch is a threading library using annotations built for android.

How to use Stitch

Using stitch is very simple. You don’t need to understand ThreadPoolExecutor or Looper. Simply add “Stitch” to your project and you are good to go. Add the following lines to your module level build.gradle (ie. app/build.gradle).

Now sync your module and gradle will automatically add Stitch to your module. Currently, there are three types of annotations which Stitch support.

CallOnAnyThread — This annotation annotates a method to execute on any thread from thread pool. This library create a fixed size thread pool on the basis of availbale processors. If all threads of the thread pool are busy, the task will be added to the task queue wait until a thread is available to process that task.

CallOnNewThread — A method which is annotated with @CallOnNewThread will always execute on a new thread. So the task will never have to wait and always start executing immediately.

CallOnUiThread — Want to do some ui changes like setting text, starting animation or anything else, simply annotate the method with @CallOnUiThread.

Any method which you want to execute on any particular thread, simply annotate that method using one of the annotations mentioned above like below.

Now, build the project using terminal or by Build -> Rebuild Project in android studio. Stitch Compiler will generate a new class file for the class in which annotated methods are. The name of generated class will be the Name of existing class in which methods are annotated+ "Stitch" . For example in above case, name of generated file will be "ExampleClassStitch" because class name is “ExampleClass”.

Next, is to get the instance of the generated class (for calling the methods). So to get the instance of generated class, you need to stitch generated class and the required class like below :

After that, you can call the method as usual and the method will execute upon the annotated thread.

and done!!!, this method will be executed on any thread from the thread pool.

Remember — Don’t make any change/update UI using @CallOnAnyThread or @CallOnNewThread otherwise android will throw an exception.

Is there any performance issue?

The answer is absolutely not and this is because this library generates the code at compile time and zero reflection is used at runtime. So there is no performance issue.

This library is open source at github. Here is the link —Stitch. For more information check README and if you found any issue just open it on github. You can contribute to this library also. For more info about contribution, read Contribution Guidelines.


I hope this article will help you in threading and make you a good developer. Thanks for reading this article. Click ❤ below to recommend this article if you found helpful and star the library on github. It would let others to get this article in their feed. Also if you have any doubt or query, feel free to ask me.

For more articles about android and other tech, follow me, you will be notified.