Multi-threading

Definition: Multi-threading is a programming and execution model that allows multiple threads to exist within the context of a single process. These threads share the process’s resources but execute independently, which can improve the performance of applications, especially on multi-core processors.

Multi-threading enables concurrent execution of code, allowing a program to perform multiple operations simultaneously. Each thread runs independently but shares the same memory space and resources, which makes communication between threads efficient but requires careful management to avoid issues such as race conditions and deadlocks.

Example:

Here is a basic example of multi-threading in JavaScript using Web Workers:

// main.js
const worker = new Worker('worker.js');

worker.onmessage = function(event) {
  console.log('Message from worker:', event.data);
};

worker.postMessage('Hello, Worker!');

// worker.js
self.onmessage = function(event) {
  console.log('Message from main script:', event.data);
  self.postMessage('Hello, Main!');
};

In this example: