Java provides built-in support for multithreading. Multithreading is the technique of performing multiple tasks simultaneously or concurrently, so it is also called multitasking. There are two types of multitasking: process-based multitasking and thread-based multitasking. Process-based multitasking means two or more processes execute at the same time which is supported by the current operating systems. Like a person is using notepad file and at the same time another file is being downloaded in the background, this is multitasking (at the process level). Now, multithreading is the technique in which single process can execute multiple tasks at the same time as a person is writing in notepad file and at the same time he is printing previous pages which he has written, means two tasks are being performed within a single process, so this is multithreading or multitasking within a single process (at thread-level). It is also called concurrent programming, where to or more parts of the program run concurrently but in time-slicing, manner means they share CPU time (in single-core CPU system). Java provides power concurrent APIs packages (also called concurrency utilities) to make concurrent programs.
Multithreading is applied by creating threads in a program or process. The thread is a part of the processor a separate execution path of the process which is executed independently of other parts or threads of the same process or program. In single-CPU system, multithreading is used in time-slicing manner, means threads share the CPU time to utilize the idle time of the CPU because most of the time processor remains idle (free) because of processes which need input from the user or they require any input/output device, so they don't continue to run and in the result, other processes wait for that process to complete. So multithreading is used to make the best use of processor idle time and write efficient programs in which, if one thread (part of the program) gets blocked for input, then other threads will not stop to work, they will continue to run independently in the time-slicing manner.
When I was not using multithreading, then how my programs were being executed?
Because java environment is made on the concept of multithreading and it is itself a multithreaded environment, so whether a multithreading is used in the program or not, there is always a main thread in the program which is automatically created with the program. Main-Thread is created for two reasons: 1) to create other (child) threads, 2) It should be the last thread to finish its execution because many shutdown tasks of the entire program are executed in the main thread.
Now, java provides two ways to create child threads in the program: 1) by extending Thread class, 2) by implementing Runnable interface. It depends on the programmer to choose between these two approaches. Thread class defines several methods to manage threads like setName(), getName(), getPriority(), start(), run(), sleep(), isAlive(), join(). Runnable interface consists of single method, which is run(). Both techniques Thread class and Runnable consist of run() method in which thread is actually executed. start() method is used to call the run() method. sleep() method is used to suspend any thread for specific time.
![Java Multithreading Multithreading In Java](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOTYXaEayt3OuqLYSeMshcH8pXJ5JluaRnFA1Vo2JR5ZNuKQZaa4dU5Rdpbc35PseIUekz6JW49u00CqZfiNXqZyOZ1kTF-2mzVI6Z8jroApnvxWH7p0SHjE76-FlK38mshf1YE0iGVWw/s320/multithreading-in-java.jpg)
Which approach should I use to create child threads? either extend Thread class or implement Runnable interface.
Because Java does not support multiple inheritance (means one class cannot extend more than one class), so a class should extend Thread class if it requires more control on threads, by using Thread class methods like getName(), getPriority(), isAlive() and join(), otherwise Runnable interface is suitable if a class only needs run() method and make a space for extending another needed class later.
Comments
Post a Comment