What is multithreading in java
Introduction: What is Multithreading
Multithreading in java refers to processing or executing multiple threads simultaneously for maximum utilization of CPU. Multithreading applications runs two or more processes concurrently.
Java Thread class allows us to create one or more lightweight threads and java executes them in parallel. The CPU of our system has access to both the memory and hardware, so it operates on both to process the tasks. The CPU core interprets the instruction and executes them and if we have more cores then more processing is happening behind the scene. If we have only one CPU in that case, then your computer still works by doing tasks switching. In tasks switching, CPU switches between tasks and performs the multiprocessing.
To know more/basic of threading, please read this tutorial first.
Concurrency
What is Concurrency?
Concurrency is the ability to do more than one thing at the same time. Running multiple parts of a single application simultaneously is also termed as concurrency.
Example:
MS Word processor formats the text and responding to keyboard and mouse events.
Concurrency doesn’t mean parallel execution. Multiple tasks executing at the same time means these multiple tasks are making progress during the same period of time. The tasks are executed in an interleaved manner. The OS switches between the tasks so frequently that it appears that they are being executed at the same time. Therefore, we can not say concurrency means parallelism.
Multiprocessing
Multiple processors/CPUs executing concurrently. Processor i.e. CPU is concurrent here.
Multitasking
Multiple tasks/process running concurrently on single CPU. The OS executes these tasks by switching in between them very frequently. Process is concurrent here.
Multithreading
Multiple parts of the same program running concurrently. In this case, we divide the same program into multiple parts/threads and going to run these threads concurrently.
Multithreading in Java Example:
package com.slb.thread;
class MultiThreadingDemo extends Thread{
@Override
public void run() {
System.out.println("Thread is running.." + Thread.currentThread().getName());
}
}
public class MultiThreadMain{
public static void main(String[] args) {
int numberOfThread = 10;
for (int i = 0; i < numberOfThread; i++){
MultiThreadingDemo multiThreadingDemo = new MultiThreadingDemo();
multiThreadingDemo.setName("Thread " + i);
multiThreadingDemo.start();
}
}
}
Output:
Thread is running..Thread 1
Thread is running..Thread 3
Thread is running..Thread 7
Thread is running..Thread 5
Thread is running..Thread 4
Thread is running..Thread 2
Thread is running..Thread 8
Thread is running..Thread 9
Thread is running..Thread 6
Thread is running..Thread 0
Process finished with exit code 0
In the above code snippet, we are creating multiple threads (10 threads we are creating). In a loop we are creating an object of a thread naming it and calling the start method on it, however you might see different output when you run this program because it depends on the which thread gets the processor and at what time.
Multithreading in java real time example
Let’s take an example of multithreading in real time:
1. Computer games: Let’s take an example of a computer game where in a lot of threads are running with some of the tasks like playing music as per character’s movement, character movements, if he is driving a car in the game then process of running the car itself is taking care by one of the tread. Similarly, in case of shooting a gun while driving same car can be handled by another thread.
2. Web Browsers: When we browse the browser it handles multiple things at the same time as we open different tabs, then on some tabs we start downloading something, etc.
3. Text Editors: While typing on the text editors we might get suggestions on spelling mistakes that is also one of the daily used real time multithreading example.
4. IDE: We have been using IDE for day to day development activity in which we write the code and at the same time a lot of other threads are processing small activities in the background like issue checks, compilation checks, builds, etc.