Multithreading Fundamentals



Programming concepts


In this article, we go over the fundamentals of multithreading.

As its name implies, multithreading allows for multiple processes or things to occur at the same time or concurrently.

It's kind of like multitasking, where a person does 2 or more things at once, such as talking on the phone, while ironing one's clothes.

A multithreaded program is a program that contains two or more parts that can run concurrently. Each part of such a program is called a thread; each thread defines a sequence path of execution.

There are two distinct types of multitasking as far as computer science is concerned.

There is process-based multitasking and thread-based multitasking.

A process is a program. Process-based multitasking is multitasking that allows a computer to run two or more programs concurrently. For example, on your computer, you can run the Internet Explorer web browser, while typing a Microsoft Word document, while running a Python program. In process-based multitasking, a program is the smallest unit of code that can be dispatched by the scheduler.

In thread-based multitasking, the thread is the smallest unit of dispatchable code. This means that a single program can perform two or more tasks at the same time. For example, you use a web browser such as Firefox and you have 2 different tabs open on two different websites. These different tabs are using separate threads.

Process-based multitasking is more common on actual computers and are used heavily in operating system.s

Thread-based multitasking is more common in programming languages. For example, in a language such as Java, multithreaded multitasking can be controlled and performed. Process-based multitasking cannot be controlled.



Advantages of Multithreading

The advantage to using multithreading is that it allows for greater efficiency for a program.

Think of it in multitasking in regular life. If you're doing two or more things at once (assuming well), you're using your time more efficiently (since more is done in a shorter time). The same can be the case for computers.

Once a computer or processor is executing a program, there is something called idle time. A lot of computer programs relies on communication. One part of a program communicates with something else. For example, a computer communicating with a disk drive or keyboard. A program communicating with a server on the internet. All of these things take time in sending or receiving signals back from these processes. This is referred to as idle time. A program waiting for a response from a server is idle time. During that time, the program could be doing something else while awaiting the response from the server. The program could be doing something else other than waiting for the response from another paritition on the computer. So while the program is awaiting a response from the server, it could be performing a calculation or buffering the next block of data to send.

This is the advantage of multithreading. Idle time isn't just wasted. Other tasks can be done during this idle time.

It is important to note that most computers today have multiple processors but not all. Some computers are single-processor systems.

For a computer that has a single processor, a programming language that has multithreading features, such as Java, cannot run multiple threads at the same time but idle time is utilized. However, with a computer that has multiple processors, as is common today, it is possible for two or more threads to run at the same time, which can further improve program efficiency and speed. This is true for actual computers that run multiple processors such as your laptop or a hosting service that runs your website with on a multiprocessor system.



Multithreading States and Features

A thread can be in several states at any given time. It can be running. It can be ready to run as soon as it gets CPU time. A thread can be suspended, which is a temporary halt of its execution. A thread can be resumed. A thread can be blocked when waiting for a resource. A thread can be terminated, in which case it ends and cannot be resumed.

Also another feature you should know about with thread-based multitasking is synchronization, which allows for the execution of threads to be coordinated, which can be useful for certain programs.


Related Resources



HTML Comment Box is loading comments...