Before knowing multithreading, first we have to know about the concept of multi-tasking. When multiple tasks are executing simultaneously, is known as multi-tasking.
For example, in a class room, while a faculty is teaching, the sitting students are doing multiple activities: such as listening the lecture, at the same time they are writing the notes, at the same time some students are in sleeping mood and at the same time some students are busy with their mobiles. So all these activities they are doing simultaneously. Hence this is called as multi-tasking. Similarly multitasking allows several activities to occur concurrently on the computer.
Basically there are two types of multitasking, such as
Executing multiple tasks simultaneously where each task is a separate independent process is known as process based multi-tasking. For example; while writing a java program in the editor, we can listen music through MP3 player. At the same time we can download a file from the internet. All these tasks are executing simultaneously and independent of each other. So it is called as process based multi-tasking. It is best suitable at operating system level.
Thread based multi-tasking is called as multithreading. Executing multiple tasks simultaneously where each task is a separate independent part of same program is known as thread based multi-tasking. It is best suitable at programmatic level. Java provides inbuilt support for thread based multi-tasking by providing varieties of library (Thread, Runnable etc...).
The purpose of multitasking is to reduce the response time and improve performance of the system whether it is process based or thread based. There are different areas where multithreading concept is used, such as video games, animation movies, multimedia graphics, server side etc..
Here I am presenting some advantages of thread-based multitasking as compared to process-based multitasking which are as follows:
Java supports thread-based multitasking and provides high-level facilities for multi-threaded programming. Thread safety is the term used to describe the design of classes that ensure that the state of their objects is always consistent, even when the objects are used concurrently by multiple threads.
class ThreadDemo { public static void main(String args[ ]) { Thread t=Thread.currentThread( ); System.out.println("current thread is"+t); System.out.println("the name of thread is"+t.getName()); } }
Output
current thread is Thread[main,5,main]
the name of thread is main
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc