|
Answer» Multithreading is a Java feature that permits the EXECUTION of two or more sections of a PROGRAM simultaneously to maximise CPU efficiency. In other words, it is the process of executing multiple threads at the same time. A thread is a component of such a program. Threads are hence lightweight processes within processes. Threads can be formed using two different mechanisms: - Extending the Thread class - We'll make a class that extends the java.lang.Thread class. The run() method of the Thread class is overridden by this class. The run() procedure is where a thread starts its life. To begin thread execution, we construct an object of our new class and use the start() method. Start() calls the Thread object's run() function.
- Bringing the Runnable Interface into Practice - We make a new class that implements the java.lang.Runnable interface and override the run() method of this. After that, we create a Thread object and call its start() method.
The advantages of multithreading are: - Since threads use a shared memory space, it helps in SAVING memory.
- Threads are autonomous and many operations can be performed at the same time, which helps in saving time.
- Because threads are independent, an exception in one thread has no impact on other threads.
|