AndroidBites: #1 Threads: Intro

Hello everyone! this is a small series I’m starting on Android Development where I will try to break down the small core programming concepts into key points and bite-size snippets which you can use to brush up your concepts!
Keep learning and Happy Hacking!
Android is a modern Operating system(OS). What makes it smart and modern is the ability to Multitask.
Multitasking
Multitasking is a process of executing multiple tasks simultaneously.
- Multitasking is of two types Process-based Multitasking (Multiprocessing)
Thread-based Multitasking (Multithreading)
Process-based Multitasking
- When we talk about a process we talking about a heavyweight task.
- They occupy an address in memory and compute everything using that.
- They mostly communicate with the OS, cost between process communication is high, lots of security and permission checks are imposed on them.
- Switching between process needs saving its state and data.
- YES, it’s an application running on your device.
Thread-based Multitasking
- A process in itself can perform many tasks using something called “threads”.
- These threads can run concurrent and execute internal task of the process independently
- You can also say Process itself a mother thread and can spawn child threads.
- This mother thread is usually called the main thread of our program, it’s the first thread that ever runs when a program starts, and when it dies the program ends.
let’s understand threads first later we can cover more on Main-thread…
Threads
A thread is a lightweight sub-process, the smallest unit of processing.
- Consider threads as separate flow execution.
- Threads are independent. If there occurs exception in one thread, it doesn’t affect other threads.
- They use the shared memory area allocated to the process.
- They always belong to a process, they need to terminate with the process and release all the resources they occupy back to the OS.
- A thread can exist whose parent process is dead, but they are alive, hold on to memory/system resource that was allocated to the parent process. The programmer should be responsible for controlling all scenarios where threads are spanned and not reclaimed.
- At a time only one thread is executed.
END
In the next article, we will look into thread creation and starting a new thread.