While learning about multi-threading in android I got confused as how are multiple threads actually executed in the cpu. I know that as a whole the cpu(single core) uses round robin algorithm to run various processes giving the delusion of parellel processing but how does the cpu manage multiple threads within a single process.
I have read that while developing apps for android we must do all resource intensive work out of the main/ui thread so that the ui does not get unresponsive while carrying out intensive work
BUT
"Since a single core cpu can execute only one thread at a time " , so whenever the cpu is busy with the worker thread then it might not be running the ui thread so during that phase won't the ui get unresponsive since cpu is busy with the worker thread.
Hence how multi-threading helps ?
I need a conceptual explanation on this possibly with an example .
*************************EDIT******************************
What happens when the user tries to interact with the ui while to worker thread is running(I know that it is rarely possible as all this happens within milli seconds but still what would happen)?

- 734
- 2
- 9
- 23
-
1possible duplicate of [Trouble Understanding CPU Scheduling Concepts](http://stackoverflow.com/questions/22084093/trouble-understanding-cpu-scheduling-concepts) – Im0rtality Aug 13 '15 at 12:46
-
2You might find it useful https://communities.intel.com/message/156694 and http://superuser.com/questions/269634/multitasking-illusion-on-a-single-threaded-processor – Rohit5k2 Aug 13 '15 at 12:47
-
2*"I know that as a whole the cpu(single core) uses round robin algorithm to run various processes giving the delusion of parellel processing but how does the cpu manage multiple threads within a single process"* The same way. – m0skit0 Aug 13 '15 at 12:50
-
@m0skit0 U mean that even threads are executed in round robin fashion ? – Shivam Aggarwal Aug 13 '15 at 12:52
-
@Im0rtality and Rohit5k2 every link says that both run concurrently , one in background and other in foreground. BUT my question is how ? – Shivam Aggarwal Aug 13 '15 at 12:55
-
Short: Each thread is given some quota of CPU time. When time expires Other thread runs on CPU. This is **NOT** parallel, but it **APPEARS** parallel. – Im0rtality Aug 13 '15 at 12:58
-
So while the worker thread is running (even for 100m sec), then during that time will the ui thread get blocked ? and what if the user tries to interact with the ui during that time(I know I sound silly but these questions always confuse me) ? – Shivam Aggarwal Aug 13 '15 at 13:03
-
@Shivamaggarwal Yes. Round-robin or whatever scheduling algorithm the CPU/OS implements. – m0skit0 Aug 13 '15 at 13:05
-
1*"So while the worker thread is running (even for 100m sec), then during that time will the ui thread get blocked ?"* Yes. *"and what if the user tries to interact with the ui during that time"* It will not respond until time is allocated for that thread, although as I said, the time slices are short enough so user doesn't notice (you can't notice 100 ms), and not all processes/threads have the same priority. – m0skit0 Aug 13 '15 at 13:08
-
And one last question that would clear all my doubts. "Suppose we are in the early 90's when cpu weren't strong enough." . Assume that we have a really slow cpu(takes 3 seconds to carry out an instruction). If I run the same program on that cpu then how would it behave. Will it work like " the ui is blocked for 3 sec then worker thread is blocked for 3 sec and so on .. ? – Shivam Aggarwal Aug 13 '15 at 13:15
2 Answers
In a single-processor system, multiple threads execute , one after the other or wait until one thread finishes or is preempted by the OS , depending on the thread priority and the OS policy.But the running threads , gives an illusion that they run simultaneous , relative to the required application response time of the User space application.
Reference : Can multithreading be implemented on a single processor system?
-
So while the worker thread is running (even for 100m sec), then during that time will the ui thread get blocked ? – Shivam Aggarwal Aug 13 '15 at 13:00
-
1check this http://stackoverflow.com/questions/7641164/android-threading-with-single-core – N Kaushik Aug 13 '15 at 13:05
-
And one last question that would clear all my doubts. "Suppose we are in the early 90's when cpu weren't strong enough." . Assume that we have a really slow cpu(takes 3 seconds to carry out an instruction). If I run the same program on that cpu then how would it behave. Will it work like " the ui is blocked for 3 sec then worker thread is blocked for 3 sec and so on .. ? – Shivam Aggarwal Aug 13 '15 at 13:16
-
1I think so ...because the overhead with context switching will surely decrease the performance...FYI Threads made an early appearance in OS/360 Multiprogramming with a Variable Number of Tasks (MVT) in 1967, in which they were called "tasks". ( https://en.wikipedia.org/wiki/Thread_(computing) ) – N Kaushik Aug 13 '15 at 13:21
-
1Wiki: " The disadvantage of preemptive multithreading is that the system may make a context switch at an inappropriate time, causing lock convoy, priority inversion or other negative effects, which may be avoided by cooperative multithreading. Cooperative multithreading, on the other hand, relies on the threads themselves to relinquish control once they are at a stopping point. This can create problems if a thread is waiting for a resource to become available." – N Kaushik Aug 13 '15 at 13:23
-
Thank you. Can you please check the other answer by Gordak , he says that while ui is blocked then all ui actions are queued up. Is that true because that way all things will get clear – Shivam Aggarwal Aug 13 '15 at 13:25
-
1That will depend on the Thread implementation for UI . ....if it is using a queue , then yes.... check this site to clear all your doubts on multithreading : http://www.nakov.com/inetjava/lectures/part-1-sockets/InetJava-1.3-Multithreading.html – N Kaushik Aug 13 '15 at 13:28
-
1
A single core processor will indeed execute only one thread at a time. The processor will switch between threads many times per second (in a given process), therefore, even if multiple threads run at the same time, they will all be given a chance to run a few times for a few milliseconds every second.
The details will depend on the thread scheduling and thread priority.
What blocks the UI thread is completely irrelevant from the processor architecture. Running a long task on the UI thread may block the thread from executing (because the processor will wait for an event to restart execution of the thread).
EDIT If the user interacts with the UI while the UI thread is not running, the UI will be unresponsive. But the actions are queued in the Thread queue. Which means that even if the thread can't handle UI actions at some point, it will still executes these tasks later. There is some limit of course.
If you plan to execute a lot of tasks, you better go for a threads pool anyway.
EDIT2
As an example, consider the very simple Activity, with a big Button which knows a lot about itself :
public class MainActivity extends ActionBarActivity {
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b = new Button(this);
b.setText("Click me. I'll make the thread sleep for one second. Then I'll notify you in the logcat when I'm done sleeping. Afterwards, the thread will execute the next task.");
setContentView(b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("COUNTER", Integer.toString(counter));
}
});
}
}
If you click this button multiple times, you'll see in the Logcat messages being printed every second. This will go on until the Thread queue is empty, meaning that onClick
has been called for each click performed.
Therefore, even if the UI thread is not running, the queue will still get UI events and you will have a chance to process them later ;)

- 2,060
- 22
- 32
-
So while the worker thread is running (even for 100m sec), then during that time will the ui thread get blocked ? – Shivam Aggarwal Aug 13 '15 at 13:00
-
1Yes. But I highly doubt that the UI thread will block for so long. The UI thread typically has a higher priority than a background thread by default. http://www.androiddesignpatterns.com/2014/01/thread-scheduling-in-android.html – Gordak Aug 13 '15 at 13:05
-
Woah. That edit helped me alot. But are you sure that the actions are queued up(Sorry but I am just confirming because that edit clarified me many things ) ? – Shivam Aggarwal Aug 13 '15 at 13:19
-
1
-
1
-
1000 Upvotes for this answer. Dude you made my day. Thank you, it cleared all my doubt queue :p . – Shivam Aggarwal Aug 13 '15 at 13:52
-
One more thing. If I use a worker thread for this sleeping task and then the ui would not get hanged up for 1 sec and then if I press the button multiple times then still would the action be queued up and executed as much times as I pressed the button ? – Shivam Aggarwal Aug 13 '15 at 13:57
-
1In short, if you have a single background thread with an associated queue: yes. In practice, things can become messy if for example you reach the limit of your queue capacity. Or if the thread executes the tasks in LIFO order (last task added is executed next), you won't see : 1,2,3,4,5... printed but something like 1,5,4,3,2 The best is to read books or good posts about that, to acquire a good overview of the system. Takes a lot of time though ... :/ – Gordak Aug 13 '15 at 14:04
-
True !!!. Another analogy- If I use a background thread and use the same sleep method to make the background thread to sleep for 1 sec . But since I am using multithreading then there will always be a switching time between threads let it be 300 milli sec . Now, I click the button and background thread runs but the cpu keeps switching ui and background threads giving me the illusion of parallel processing but won't the switching time also get added to that 1 second , finally giving me result after 1 sec + 300 milli sec . – Shivam Aggarwal Aug 13 '15 at 14:16
-
1Well yes and no. It's true that sleep isn't really accurate, but here we are diving into thread scheduling and we can't make simple assumptions anymore. That's a huuuuge domain on its own and I can't answer it for two reasons: 1) Answer would be too complex to fit in a 600 characters response 2) I don't know enough on Linux/Android process/thread scheduling ;) Best is to read about it. I read the entire "Computer Systems : a programmer's perspective" book, but even in there they don't go into details. – Gordak Aug 13 '15 at 14:39
-