Concurrency vs multithreading in Java
Concurrency and multithreading are two common terms used when talking about programming languages, especially Java. While both are related to the concept of threading, there are subtle differences between them. This article will explore the differences between concurrency and multithreading in Java.
Concurrency
Concurrency is the ability of a program to execute multiple tasks simultaneously. It means that a program can have multiple threads that are running at the same time. A program can have multiple tasks running in parallel, and they can all interact with each other.
One of the main advantages of having concurrency in a program is that it can help increase its performance. This is because it allows the program to run multiple tasks at the same time, thus reducing the amount of time it takes for a task to finish.
In Java, concurrency is implemented using the java.util.concurrent package. This package provides several classes and interfaces that allow developers to create and manage concurrent tasks. It also provides the Executor framework, which is a high-level abstraction for managing the tasks.
Multithreading
Multithreading is the ability of a program to execute multiple threads at the same time. A thread is a single sequence of instructions within a program. When multiple threads are running at the same time, they are said to be executing in parallel.
Multithreading is a great way to improve the performance of a program. This is because multiple threads are able to use the same resources, such as memory and processor time, which would otherwise be wasted if only one thread was running.
In Java, multithreading is implemented using the java.lang.Thread class. The class provides the basic methods for creating and managing threads. It also provides the ThreadPoolExecutor class, which is a high-level abstraction for managing the threads.
Difference Between Concurrency and Multithreading
Concurrency | Multithreading |
Concurrency is a general concept used to describe a program’s ability to execute multiple tasks simultaneously. | Multi-Threading is a specific implementation of concurrency that is used to execute multiple threads at the same time. |
Concurrency can be implemented using the Executor framework. | Multithreading requires using the Thread class. |
Program for Concurrency
Concurrency.java
// Main class declaration
public class Concurrencyimplements Runnable {
// default run method in thread class
public void run() {
System.out.println("Thread " + Thread.currentThread().getId() + " is running");
}
// main class of the program from where execution begins
public static void main(String[] args) {
int n = 8; // Number of threads
for (int i=0; i<n; i++) {
Thread object = new Thread(new Concurrency());
object.start();
}
}
}
Output
Thread 12 is running
Thread 15 is running
Thread 16 is running
Thread 14 is running
Thread 11 is running
Thread 10 is running
Thread 13 is running
Thread 17 is running
Note: The output of the above program will change after each execution.
Explanation
The above example creates a Concurrency class that extends the Thread class.
The run() method is overridden and contains the code that the thread will execute when it is started.
The main() method creates an instance of the thread class and calls its start() method.
This will cause the thread to begin execution.
Program for Multi-Threading
class MultithreadingDemo extends Thread
{
public void run()
{
try
{
// Displaying the thread that is running
System.out.println ("Thread " +
Thread.currentThread().getId() +
" is running");
}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught");
}
}
}
// Main Class
public class MultiThreadingDemo
{
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i=0; i<8; i++)
{
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}
}
}
Output:
Thread 17 is running
Thread 16 is running
Thread 10 is running
Thread 13 is running
Thread 12 is running
Thread 14 is running
Thread 15 is running
Thread 11 is running
Note: The output of the above program will change after each execution.
Explanation
The program uses the Thread class to create a thread.
The program creates an object of the class and calls the start() method of the Thread class which calls run() method by default.
Inside the run() method, we have printed the ID of the thread.
The main() method creates 8 threads and each thread starts executing the run() method.
Conclusion
In conclusion, concurrency and multithreading are related concepts used when talking about programming languages, especially Java. Concurrency is the general concept of executing multiple tasks simultaneously, while multithreading is the specific implementation of concurrency used to execute multiple threads simultaneously. The main difference between the two is that concurrency can be implemented using the Executor framework, while multithreading requires using the Thread class.