PCPF Experiment No - 12 AIM: Examples
PCPF Experiment No - 12 AIM: Examples
a. Creation of thread
b. Thread running
THEORY :
It’s a programming concept where a Java program (process) is divided into two or more sub-
programs (sub-processes) which are implemented (executed) at the same time in parallel and such
sub-programs are called threads.
In simple words a thread is similar to a program that has starting point, body and ending point.
Threads are lightweight processes that share the same memory space of its parent process. It
allows concurrent execution of two or more parts of a program for maximum utilization of CPU.
In Java, JVM handles the switching of control between the threads in such a way that it appears
threads are running concurrently.
Examples : :
Advantages : :
2. By extending java.lang.Thread.
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 Synchronization
We want only one thread should access that common resource at a time. Hence we require
synchronization.
CODE :
OUTPUT:
CONCLUSION: