Processes and threads - Gary explains

**The Life Cycle of an App: Understanding Multi-Threading**

In today's digital age, applications are becoming increasingly complex, with multiple tasks running concurrently to provide a seamless user experience. A system, for example, that displays a game on the screen while sending information up to the internet about the character's position and state, needs to manage these concurrent tasks efficiently. To achieve this, programmers employ various techniques, including multi-threading.

**What is Multi-Threading?**

Multi-threading allows an app to have multiple ways of execution through the program, enabling it to perform multiple tasks simultaneously without compromising performance. This technique enables a system to run multiple apps, each with its own logical container called a process, which has an ID, priority, memory space, and status zone. The kernel uses this information to allocate slices of CPU time to every app that wants to run, allowing them to appear as if they're running simultaneously, even on multi-core machines.

**Threads of Execution**

In each process, there can be multiple threads of execution, which enable the program to perform multiple tasks at once, such as updating the game and sending network information. To prevent conflicts between these threads, developers use a technique called locking. Locking allows a thread to acquire exclusive access to a particular section of code, ensuring that no other thread modifies the data simultaneously.

**The Problem with Unlocked Threads**

If two threads execute the same piece of code without locking, disaster can strike. One thread may put a value into a variable, while another thread puts zero in there. When the first thread returns, it expects to find the original value but instead finds zero, causing errors and chaos. This scenario highlights the importance of proper locking in multi-threading.

**Locking: The Solution**

To mitigate this problem, developers use locks to prevent multiple threads from accessing sensitive areas of code simultaneously. A lock is essentially a mechanism that allows one thread to gain exclusive access to a particular section of code, while preventing other threads from entering it until the first thread has finished. Locks come in various forms and work differently depending on the implementation.

**Deadlocks: The Enemy of Multi-Threading**

While locks help prevent conflicts between threads, they can also lead to another problem called deadlocks. A deadlock occurs when two or more threads wait for each other to release a resource, resulting in an infinite loop of waiting. This scenario can be particularly disastrous, as it brings the entire app to a grinding halt.

**Conclusion**

Multi-threading is a crucial technique in app development, allowing developers to create complex and efficient systems that manage concurrent tasks effectively. However, proper locking is essential to prevent conflicts between threads, while deadlocks must be avoided at all costs. By understanding these concepts and implementing them correctly, programmers can create high-performance apps that provide a seamless user experience.

**About the Author**

My name's Gary Sims from Android or throaty. I hope you enjoyed this video if you did please do give it a thumbs up! If you're interested in learning more about multi-processing, Linux threading, and app development using threads, check out the Android or 'ti forums. We have a whole section there where you can connect with me, talk about threads, discuss kernels, and even ask questions about processes. I'm there if you want to chat about any of these topics.

**Get Involved**

In the comments below, share your thoughts on multi-processing, Linux threading, and app development using threads. Have you ever worked on an app that utilized threads? Let me know! Don't forget to stay tuned to Android Authority as we are your source for all things Android.

"WEBVTTKind: captionsLanguage: enhello my name is Gary sim from handle authority now I'm fairly sure that you know that your smartphone can run more than one program at a time even when you're running just a game or something you've got an app in the foreground you realize there are things going on in the background you get notifications you've got an email someone's mentioned you on Twitter and app is being updated all these things are happening while you're using the app in the foreground so therefore your smartphone is actually running multiple programs now if you've ever wondered how that works and please let me explain so first of all bit of background information inside your smartphone there are three very important components when it comes to running apps but of course it's the CPU the other is the round the random access memory and the other is the memory controller which is the interface between the CPU and the RAM now when you have a simple computer let's say a microcontroller used to display numbers on your microwave oven life is really simple there's one program to run all of the CPU time is given over to that program and all the RAM is given over to that program really easy but when you want to run two programs or ten programs things now become more complicated because the memory and the CPU time need to be divided up amongst those programs so how does the Linux kernel do that well this is where we get the idea of a process it's a logical container which holds all the information about each of the apps that are running on your smartphone so in there will be the name of the app it's binary where it can be found it's priority each one has its own unique IDs called the P the process ID there is the list of its memory where is it available in memory and it has a state is it running is it sleeping as it stopped is it zone beat now but once the kernel has all this information about all the apps that need to run it can start to schedule different resources for each of those apps so when an app is sleeping it doesn't need any cpu time we can just leave it alone but when an app wants to run the kernel says ok you can now have a slice of CPU time and then another app says hey I want a slice of CPU time so the colors well you can have a slice of cpu time now this is all happening at the millisecond level and like a cartoon which shows one frame after the other frame very quickly and gives us the idea of animation running little bits of each program very quickly gives us the appearance that's actually running things in parallel now of course a day we actually use CPUs with multiple cores four cores or eight cores so the CPU actually can run things in parallel but the good news is the concept of a process and the fact that it needs memory the faculty needs CPU time remains exactly the same but now the kernel has more CPU resources to give to each of those processes now normally when a process is created there is a single thread of execution through that program every process has a life cycle is a beginning it runs goes through the middle of it runs around and whatever it needs to do and then finally there's an end where it exits and goes away now if you have a game for example that you're writing and you're displaying lots of things on the display but at the same time you want to send information up to the internet about the position of your character the state of the character it may be a massive online playing game then now that app needs to do two things at once now there are ways of programming that where you can do two things at once but there's also a system provided by the kernel that allows each app to have multiple ways of execution through the program so while one path through the program is updating the game another path through the program is sending the network information up and that's called a thread multiple threads of execution now if you've been understood what I've said you may come and say to me but hold on Gary what happens if the same two threads come to execute the same bit of code and you would be right that would be disastrous because one throughputs let's say the number 10 into a particular variable the other 3 puts zero in there when the first thread comes back there again it was expecting a 10 but now it's a zero and always goes wrong there's just absolute chaos so to get round this there's the idea of locking that means that when a particular piece of code it is important it can be locked and there are a whole variety of different locks that are available and the way they work is different but the basic idea is this thread one comes into an area that is sensitive it will lock that area and say no one else can come in here it will do its work and while if that's happening thread number two comes along it will wait until threat number one has finished and then exits out the other end but of course then if that's not done right then the whole idea of multi-threading just disappears because rather than having multiple threads all running in parallel in your program what actually happens if they all end up queuing up one behind the other because they're all waiting for this important section so actually getting your locking right in a thread is very important and there are other problems for example what happens if a thread leaves that section under an error condition and because of a bug in the program it forgets to unlock the code then now it will wait forever because the code remains locked there are also other problems one if you've got two threads thread number one enters into a section of code that's locked and it then starts waiting for something from thread number two straight number two comes into a program area it locks it but it can't proceed until it's able to go into the code that is currently occupied by thread number one or thread number one is now waiting for threat number two and thread number two is now waiting for thread number one and we get deadlock so although these operating systems Linux and so on give us multi-threading that give us multi processes actually from a programmers point of view it's important to get them right because if you get them wrong your app actually won't behave any better in fact it will behave much worse so let me give you a brief summary because a Linux kernel Android can run multiple apps the Linux kernel needs to have a logical container that tells it which app terrain that's called a process and the presence has an ID has a priority it has some memory space it has a status zone and the kernel uses that information to give slices of CPU time to every single app that wants to run and because it does is a millisecond level each app gets the appearance it's running similar seriously with all the other apps and even on multi-core machines that's exactly the same just now there are more calls that can execute each of the programs inside each process there can be multiple threads of execution so that each program can do multiple things at once update the internet while updating the game and to make sure there's no conflict of interest inside those threads is a thing called locking and locking allows the program of the app developer to ensure that data isn't modified by two threads simultaneously well my name's Gary Sims from Android or throaty and I hope you enjoyed this video if you did please do give it a thumbs up I really would recommend you go to the Android or 'ti forums there's a whole section there where you can connect with me you can talk about threads you can talk about kernels we could talk about processes I'm there if you want to talk to me about any of those also please don't forget to use the comments below to tell us what you think about multi-processing Linux threading have you ever done the app development using threads let me know in the comments below also don't forget to stay tuned to android authority because we are your source for all things Androidhello my name is Gary sim from handle authority now I'm fairly sure that you know that your smartphone can run more than one program at a time even when you're running just a game or something you've got an app in the foreground you realize there are things going on in the background you get notifications you've got an email someone's mentioned you on Twitter and app is being updated all these things are happening while you're using the app in the foreground so therefore your smartphone is actually running multiple programs now if you've ever wondered how that works and please let me explain so first of all bit of background information inside your smartphone there are three very important components when it comes to running apps but of course it's the CPU the other is the round the random access memory and the other is the memory controller which is the interface between the CPU and the RAM now when you have a simple computer let's say a microcontroller used to display numbers on your microwave oven life is really simple there's one program to run all of the CPU time is given over to that program and all the RAM is given over to that program really easy but when you want to run two programs or ten programs things now become more complicated because the memory and the CPU time need to be divided up amongst those programs so how does the Linux kernel do that well this is where we get the idea of a process it's a logical container which holds all the information about each of the apps that are running on your smartphone so in there will be the name of the app it's binary where it can be found it's priority each one has its own unique IDs called the P the process ID there is the list of its memory where is it available in memory and it has a state is it running is it sleeping as it stopped is it zone beat now but once the kernel has all this information about all the apps that need to run it can start to schedule different resources for each of those apps so when an app is sleeping it doesn't need any cpu time we can just leave it alone but when an app wants to run the kernel says ok you can now have a slice of CPU time and then another app says hey I want a slice of CPU time so the colors well you can have a slice of cpu time now this is all happening at the millisecond level and like a cartoon which shows one frame after the other frame very quickly and gives us the idea of animation running little bits of each program very quickly gives us the appearance that's actually running things in parallel now of course a day we actually use CPUs with multiple cores four cores or eight cores so the CPU actually can run things in parallel but the good news is the concept of a process and the fact that it needs memory the faculty needs CPU time remains exactly the same but now the kernel has more CPU resources to give to each of those processes now normally when a process is created there is a single thread of execution through that program every process has a life cycle is a beginning it runs goes through the middle of it runs around and whatever it needs to do and then finally there's an end where it exits and goes away now if you have a game for example that you're writing and you're displaying lots of things on the display but at the same time you want to send information up to the internet about the position of your character the state of the character it may be a massive online playing game then now that app needs to do two things at once now there are ways of programming that where you can do two things at once but there's also a system provided by the kernel that allows each app to have multiple ways of execution through the program so while one path through the program is updating the game another path through the program is sending the network information up and that's called a thread multiple threads of execution now if you've been understood what I've said you may come and say to me but hold on Gary what happens if the same two threads come to execute the same bit of code and you would be right that would be disastrous because one throughputs let's say the number 10 into a particular variable the other 3 puts zero in there when the first thread comes back there again it was expecting a 10 but now it's a zero and always goes wrong there's just absolute chaos so to get round this there's the idea of locking that means that when a particular piece of code it is important it can be locked and there are a whole variety of different locks that are available and the way they work is different but the basic idea is this thread one comes into an area that is sensitive it will lock that area and say no one else can come in here it will do its work and while if that's happening thread number two comes along it will wait until threat number one has finished and then exits out the other end but of course then if that's not done right then the whole idea of multi-threading just disappears because rather than having multiple threads all running in parallel in your program what actually happens if they all end up queuing up one behind the other because they're all waiting for this important section so actually getting your locking right in a thread is very important and there are other problems for example what happens if a thread leaves that section under an error condition and because of a bug in the program it forgets to unlock the code then now it will wait forever because the code remains locked there are also other problems one if you've got two threads thread number one enters into a section of code that's locked and it then starts waiting for something from thread number two straight number two comes into a program area it locks it but it can't proceed until it's able to go into the code that is currently occupied by thread number one or thread number one is now waiting for threat number two and thread number two is now waiting for thread number one and we get deadlock so although these operating systems Linux and so on give us multi-threading that give us multi processes actually from a programmers point of view it's important to get them right because if you get them wrong your app actually won't behave any better in fact it will behave much worse so let me give you a brief summary because a Linux kernel Android can run multiple apps the Linux kernel needs to have a logical container that tells it which app terrain that's called a process and the presence has an ID has a priority it has some memory space it has a status zone and the kernel uses that information to give slices of CPU time to every single app that wants to run and because it does is a millisecond level each app gets the appearance it's running similar seriously with all the other apps and even on multi-core machines that's exactly the same just now there are more calls that can execute each of the programs inside each process there can be multiple threads of execution so that each program can do multiple things at once update the internet while updating the game and to make sure there's no conflict of interest inside those threads is a thing called locking and locking allows the program of the app developer to ensure that data isn't modified by two threads simultaneously well my name's Gary Sims from Android or throaty and I hope you enjoyed this video if you did please do give it a thumbs up I really would recommend you go to the Android or 'ti forums there's a whole section there where you can connect with me you can talk about threads you can talk about kernels we could talk about processes I'm there if you want to talk to me about any of those also please don't forget to use the comments below to tell us what you think about multi-processing Linux threading have you ever done the app development using threads let me know in the comments below also don't forget to stay tuned to android authority because we are your source for all things Android\n"