Concurrency in programming refers to the ability of a program to execute multiple tasks or processes simultaneously. Imagine you're playing a game on your computer while listening to music in the background. Concurrency allows your computer to run both the game and the music player at the same time, giving you the illusion that they are running simultaneously.
In programming, concurrency is essential for improving efficiency and responsiveness, especially in applications that need to handle multiple tasks concurrently, such as web servers, operating systems, and multimedia applications.
Here's a simple example in Java to illustrate concurrency using threads:
public class ConcurrencyExample {
public static void main(String[] args) {
// Create two threads
Thread thread1 = new Thread(new Task("Task 1"));
Thread thread2 = new Thread(new Task("Task 2"));
// Start the threads
thread1.start();
thread2.start();
}
}
class Task implements Runnable {
private String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
// Simulate some task execution
for (int i = 0; i < 5; i++) {
System.out.println(name + " is executing iteration " + i);
try {
// Introduce a short delay to simulate work
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + " is complete");
}
}
In this example, we have a Task
class that implements the Runnable
interface, which represents a task that can be executed by a thread. The run()
method of the Task
class simulates some work by printing a message and then sleeping for 1 second in each iteration.
In the main
method, we create two threads, each associated with an instance of the Task
class (Task 1
and Task 2
). We then start both threads concurrently using the start()
method. As a result, both tasks run concurrently, and you'll see output from both tasks interleaved in the console.