0% found this document useful (0 votes)
38 views2 pages

PCPF Experiment No - 12 AIM: Examples

This document describes thread management in Java. It defines threads as sub-programs that can run concurrently within a process, sharing the same memory. Threads allow for maximum CPU utilization. The document discusses how to create threads by implementing Runnable or extending Thread, and overriding the run() method. It also covers thread synchronization using synchronized methods or blocks to ensure only one thread accesses common resources at a time.

Uploaded by

Anvay Gorule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

PCPF Experiment No - 12 AIM: Examples

This document describes thread management in Java. It defines threads as sub-programs that can run concurrently within a process, sharing the same memory. Threads allow for maximum CPU utilization. The document discusses how to create threads by implementing Runnable or extending Thread, and overriding the run() method. It also covers thread synchronization using synchronized methods or blocks to ensure only one thread accesses common resources at a time.

Uploaded by

Anvay Gorule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PCPF EXPERIMENT NO – 12

AIM : Demonstrate thread management in Java:

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 : :

1. In Google Chrome, multiple tabs are opened.


2. Printing can be done in the background.
3. In MS Word, auto save and spell checker working concurrently.
4. Windows Media Player.

Advantages : :

1. It improves the speed of program execution.


2. Multiple tasks can be done at same time.
3. Minimized system resource usage. Threads impose minimal impact on system resources i.e.
threads require less overhead to create, maintain, and manage than a traditional process.
4. Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.
5.  It doesn't block the user as they are independent and one can perform multiple operations
at the same time.

In Java we have two methods/options to achieve Multithreading.

1. By implementing java.lang.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.

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.

In both the cases it’s compulsory to override run()


run() is the entry point for the new thread. It makes up the entire body of a thread.

Thread Synchronization

We want only one thread should access that common resource at a time. Hence we require
synchronization.

It can be achieved in two ways.

1. Using Synch methods


public synchronized void run()
{

2. Using Sync blocks.


synchronized(this) //means current object
{

CODE :

OUTPUT:

CONCLUSION:

You might also like