0% found this document useful (0 votes)
8 views30 pages

Java Multithreading Concepts Explained

Java

Uploaded by

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

Java Multithreading Concepts Explained

Java

Uploaded by

mohamedjaaved19
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Thread

TNS India Foundation | ‹#›


Partners in Economic Transformation
Recap

• Threads: Independent sequences of instructions within a program that can run


concurrently.
• Multithreading: Concurrent execution of multiple threads within a single
program, enabling parallel processing and improved performance.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. What is the initial state of a thread in the Java thread life cycle?

A Running

B Ready

C Blocked

D New

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. What is the initial state of a thread in the Java thread life cycle?

A Running

B Ready

C Blocked

D New

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. What is a thread in Java?

A A lightweight process

B A parallel execution unit

C A component of the Java Virtual Machine (JVM)

D All of the above

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. What is a thread in Java?

A A lightweight process

B A parallel execution unit

C A component of the Java Virtual Machine (JVM)

D All of the above

TNS India Foundation | ‹#›


Partners in Economic Transformation
Learning Objective

● Achieve synchronization in Java threads.


● Explore inter-thread communication mechanisms.
● Address the consumer and producer problem in multithreading.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Activity in Threads

● Engaged in Microsoft Office.


● Explored the functionalities of Internet Explorer.
● Delved into the Eclipse IDE environment.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Synchronization

● Synchronization in java is the capability to control the access of multiple threads to


any shared resource.
● In the Multithreading concept, multiple threads try to access the shared resources at
a time to produce inconsistent results.
● The synchronization is necessary for reliable communication between threads.
Example: Reading a record from a file while another thread is still writing the same file.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Types of synchronization

Synchronization is classified into two types.

● Process Synchronization
● Thread Synchronization

Process Synchronization:

● The process is nothing but a program under execution.


● It runs independently isolated from another process.
● The resources like memory and CPU time, etc. are allocated to the process by the
operating System.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Thread Synchronization

Thread synchronization is two types,

[Link] Exclusive:

● A Mutex or Mutual Exclusive helps only one thread to access the shared resources.
● It won’t allow the accessing of shared resources at a time.
● It can be achieved in the following ways.
● Synchronized Method
● Synchronized block
● Static Synchronization

TNS India Foundation | ‹#›


Partners in Economic Transformation
Synchronized Method

● When we declare a synchronized keyword in the header of a method, it is called


synchronized method.
● Once a method is made synchronized, and thread calls the synchronized method, it
gets locked on the method.
● All other threads will have to wait for the current thread to release the lock.
● Using the synchronized keyword, we can synchronize the entire method.
synchronized void show()
{
// statements to be synchronized

TNS India Foundation | ‹#›


Partners in Economic Transformation
Synchronized Block

Synchronizing a block of code is another way of controlling the execution of thread.


Syntax:
synchronized(object)
{
// statements to be synchronized
}

TNS India Foundation | ‹#›


Partners in Economic Transformation
Why we use Synchronization

● Synchronization helps in preventing thread interference.


● Synchronization helps to prevent concurrency problems.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Real Time Example

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
InterThread Communication in Java

● Inter-thread communication in Java is a technique through which multiple threads


communicate with each other.
● It provides an efficient way through which more than one thread communicate with
each other by reducing CPU idle time. CPU idle time is a process in which CPU cycles
are not wasted.
● Example: Suppose that there are two threads A and B. Thread B uses data produced
by Thread A and performs its task.
● If Thread B waits for Thread A to produce data, it will waste many CPU cycles.
● But if threads A and B communicate with each other when they have completed their
tasks, they do not have to wait and check each other’s status every time.

TNS India Foundation | ‹#›


Partners in Economic Transformation
How to achieve Inter thread communication in Java

● Inter thread communication in Java can be achieved by using three methods provided
by Object class of [Link] package.
■ wait()
■ notify()
■ notifyAll()
● These methods can be called only from within a synchronized method or synchronized
block of code otherwise, an exception named IllegalMonitorStateException is thrown.
● All these methods are declared as final.
● Since it throws a checked exception, use these methods within Java try-catch block.

TNS India Foundation | ‹#›


Partners in Economic Transformation
wait() method

● wait() method in Java notifies the current thread to give up the monitor (lock) and to go
into sleep state until another thread wakes it up by calling notify() method.
● This method throws InterruptedException.

Syntax:
public final void wait()
public final void wait(long millisecond) throws InterruptedException
public final void wait(long millisecond, long nanosecond) throws InterruptedException

TNS India Foundation | ‹#›


Partners in Economic Transformation
notify() ,notifyAll()
● The notify() method wakes up a single thread that called wait() method on the same
object.
● If more than one thread is waiting, this method will awake one of them.
Syntax:

public final void notify()

● The notifyAll() method is used to wake up all threads that called wait() method on the
same object.
● The thread having the highest priority will run first.
Syntax:
public final void notifyAll()

TNS India Foundation | ‹#›


Partners in Economic Transformation
Hands on Coding

TNS India Foundation | ‹#›


Partners in Economic Transformation
Consumer -Producer Problem

● The producer-consumer problem in Java (also known as the bounded-buffer problem)


is a classic multi-process synchronization problem, in which we try to achieve
synchronization between more than one process.

● The producer should produce data only when the buffer is not full. In case it is found
that the buffer is full, the producer is not allowed to store any data into the memory
buffer.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Consumer -Producer Problem

● Data can only be consumed by the consumer if and only if the memory buffer is not
empty. In case it is found that the buffer is empty, the consumer is not allowed to use
any data from the memory buffer.
● Accessing memory buffer should not be allowed to producer and consumer at the
same time.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Let’s Reflect -

1. What is your major learning from today’s session?


2. How are you going to use this learning in your journey on the job?

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. Which keyword is used to implement synchronization in Java?

A sync

B synchronized

C lock

D threadsafe

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q1. Which keyword is used to implement synchronization in Java?

A sync

B synchronized

C lock

D threadsafe

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. Which methods are commonly used for inter-thread communication in


Java?

A send() and receive()

B notify() and wait()

C start() and stop()

D suspend() and resume()

TNS India Foundation | ‹#›


Partners in Economic Transformation
Quiz

Q2. Which methods are commonly used for inter-thread communication in


Java?

A send() and receive()

B notify() and wait()

C start() and stop()

D suspend() and resume()

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question
Objective:

Apply multithreading concepts to enhance efficiency and responsiveness in a banking


system.

Task 1: Conceptual Understanding

● Explain challenges faced by a banking system without multithreading.


● Describe how threads can address challenges and improve system performance.
● Identify potential states in the Thread Lifecycle during transaction execution.

TNS India Foundation | ‹#›


Partners in Economic Transformation
Assignment Question
Task 2: Implementation

● Implement a Java program for a simplified banking system with customer accounts and
concurrent transactions.
● Extend the program to demonstrate synchronization techniques for shared resources.

Task 3: Testing and Analysis

● Conduct tests with concurrent transactions to showcase multithreading benefits.


● Analyze challenges in a multithreaded banking system and propose solutions.

TNS India Foundation | ‹#›


Partners in Economic Transformation

You might also like