Practical No. 13 : Write a program in Java to implement Multithreading.
1. Implement a simple program which will just have a main thread.
Implement all methods in it like current Thread, sleep and setName.
2. Create a thread using runnable interface.
3. Create thread using thread class.
1.1 Multithreading
Multithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU. Each part of such program is called a thread. So,
threads are light-weight processes within a process. Threads can be created by using two
mechanisms :
1.Extending the Thread class
2.Implementing the Runnable Interface
Thread creation by extending the Thread class
We create a class that extends the java.lang.Thread class. This class overrides the run() method
available in the Thread class. A thread begins its life inside run() method. We create an object
of our new class and call start() method to start the execution of a thread. Start() invokes the
run() method on the Thread object
Thread creation by implementing the Runnable Interface
We create a new class which implements java.lang.Runnable interface and override run()
method. Then we instantiate a Thread object and call start() method on this object.
1.2 Thread Class vs Runnable Interface
1. If we extend the Thread class, our class cannot extend any other class because Java doesn’t
support multiple inheritance. But, if we implement the Runnable interface, our class can still
extend other base classes.
2. We can achieve basic functionality of a thread by extending Thread class because it provides
some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.
3. Using runnable will give you an object that can be shared amongst multiple threads.
1.3 Advantages of Java Multithreading
1) It doesn't block the user because threads are independent and you can perform multiple
operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.
1.4 Thread in Java
A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of
execution.
Threads are independent. If there occurs exception in one thread, it doesn't affect other threads.
It uses a shared memory area.
As shown in the above figure, a thread is executed inside the process. There is contextswitching
between the threads. There can be multiple processes inside the OS, and one process can have
multiple threads.
1.5 Java Thread class
Java provides Thread class to achieve thread programming. Thread
class provides constructors and methods to create and perform operations on a thread.
Thread class extends Object class and implements Runnable interface.
1.Implement a simple program which will just have a main thread. Implement all methods
in it like currentThread, sleep, and Setname
// Source Code
Fig: Source Code
// Output
Fig: Output
2.Create a thread using runnable interface.
// Source Code
Fig: Source Code
// Output
Fig: Output
3.Create thread using thread class.
// Source Code
Fig: Source Code
// Output
Fig: Output