Sobes.tech
Junior — Middle

Can you explain what is meant by multithreading in programming?

sobes.tech AI

Answer from AI

Multithreading in programming is the ability of a program to execute multiple threads concurrently. Each thread is a separate sequence of commands that can run in parallel with others.

In Python, multithreading is often used to improve performance during input-output operations (such as network requests, reading files), because due to the GIL (Global Interpreter Lock), true parallel execution of threads with the CPU is limited.

Example of creating and starting threads in Python:

import threading

def worker(num):
    print(f"Thread {num} has started")

threads = []
for i in range(5):
    t = threading.Thread(target=worker, args=(i,))
    threads.append(t)
    t.start()

for t in threads:
    t.join()

Here, 5 threads are created, which execute the worker function in parallel.