Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 2.3 KB

File metadata and controls

42 lines (33 loc) · 2.3 KB

Scalability

Scalability in programming refers to the ability of a system to handle increasing amounts of work or traffic by adding resources or by adapting to changing needs without sacrificing performance. It involves designing systems in such a way that they can efficiently scale up or down in response to varying workloads.

Here's a simple example in Java illustrating scalability using a thread pool to handle multiple tasks:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ScalabilityExample {
    public static void main(String[] args) {
        // Create an executor service with a cached thread pool
        ExecutorService executor = Executors.newCachedThreadPool();

        // Submit tasks to the executor
        for (int i = 0; i < 10; i++) {
            int taskId = i;
            executor.submit(() -> {
                // Simulate some task execution
                System.out.println("Task " + taskId + " is executing");
                try {
                    // Introduce a short delay to simulate work
                    Thread.sleep(1000); // Sleep for 1 second
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Task " + taskId + " is complete");
            });
        }

        // Shutdown the executor service
        executor.shutdown();
    }
}

In this example, we use a CachedThreadPool executor service, which dynamically adjusts the pool size based on the workload. As tasks are submitted to the executor, it will create new threads as needed, up to a maximum limit, or reuse existing idle threads if available.

We submit 10 tasks to the executor in a loop. Each task simulates some work by printing messages and sleeping for 1 second. Since we're using a thread pool, these tasks can be executed concurrently, with the executor managing the allocation and reuse of threads.

By using a thread pool, this example demonstrates scalability because the system can handle an increasing number of tasks efficiently without needing to manually manage thread creation or resource allocation. As the workload increases, the thread pool can scale up to allocate more threads, ensuring that tasks are processed in a timely manner without overwhelming the system.