Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do multiple threads run on single core cpu

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)?

like image 726
Shivam Aggarwal Avatar asked Aug 13 '15 12:08

Shivam Aggarwal


People also ask

How many threads can run on a single core?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.

How does a single core CPU multitask?

Single Core In the case of a computer with a single CPU core, only one task runs at any point in time, meaning that the CPU is actively executing instructions for that task. Multitasking solves this problem by scheduling which task may run at any given time and when another waiting task gets a turn.

What allows CPU to handle multiple threads simultaneously?

Symmetric Multiprocessing and Symmetric Multithreading are two different things. To put it simply: SMT = The CPU can execute more than one thread simultaneously, by scheduling a second thread that can use the execution units not currently in use by the first thread.


1 Answers

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 ;)

like image 191
Gordak Avatar answered Sep 28 '22 08:09

Gordak