0% found this document useful (0 votes)
24 views

Chapter 5

This document discusses single-threaded and multi-threaded programming in Java. It explains that single-threaded programs can only perform one task at a time, while multi-threaded programs allow for concurrent execution of multiple tasks. There are two types of multitasking: process-based, where each program is a separate task, and thread-based, where a single program can have multiple internal tasks. The document then covers key aspects of thread-based multitasking in Java like creating and running threads, synchronization techniques like synchronized methods and blocks, and inter-thread communication methods like wait(), notify(), and notifyAll().

Uploaded by

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

Chapter 5

This document discusses single-threaded and multi-threaded programming in Java. It explains that single-threaded programs can only perform one task at a time, while multi-threaded programs allow for concurrent execution of multiple tasks. There are two types of multitasking: process-based, where each program is a separate task, and thread-based, where a single program can have multiple internal tasks. The document then covers key aspects of thread-based multitasking in Java like creating and running threads, synchronization techniques like synchronized methods and blocks, and inter-thread communication methods like wait(), notify(), and notifyAll().

Uploaded by

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

Chapter 5: thread

• We can walk, talk, breathe, see, hear, smell... all at the same time

• Computers can do this as well - download a file, print a file, receive email, run the
clock

• Only computers that have multiple processors can truly execute multiple instructions
concurrently

Two types of executing tasks:

• Single Tasking: Executing only one task at a time.

• In this single tasking the microprocessor will be sitting idle for most of the time. This
means micro processor time is wasted.

• Multi tasking: Executing more than one task at a time


Multi tasking
• there are two distinct types of multitasking: process-based and thread-based.
• A process is a program that is executing.

• Process-based multitasking is the feature that allows your computer to run


two or more programs concurrently.

• For example, process based multitasking enables you to run and use Microsoft
words at the same time you can use a text editor or visiting a web site.

• In process-based multitasking, a program is the smallest unit of code that can

be dispatched by the scheduler.


Multitasking(cont’d…)
• In a thread-based multitasking environment, the thread is the smallest unit of
dispatchable code.

• This means that a single program can perform two or more tasks
simultaneously.

• For instance, a text editor can format text at the same time that it is printing,
as long as these two actions are being performed by two separate threads.

• Thus, process-based multitasking deals with the “big picture” and thread-
based multitasking handles the details.
Single thread and multithread

• Single-threaded systems use an approach called an event loop with polling.

• In this model, a single thread of control runs in an infinite loop, polling a single
event queue to decide what to do next.

• For instance, a network file is ready to be read, then the event loop dispatches
control to the appropriate event handler.
• Until this event handler returns, nothing else can happen in the program.

• This wastes CPU time.

• It can also result in one part of a program dominating the system and preventing any
other events from being processed.
Single threaded and multithreaded
• In general, in a single-threaded environment, when a thread blocks (that is, suspends
execution) because it is waiting for some resource, the entire program stops running.

• The benefit of Java’s multithreading is that the main loop/polling mechanism is


eliminated. One thread can pause without stopping other parts of your program.

• For example, the idle time created when a thread reads data from a network or waits
for user input can be utilized elsewhere.

• Multithreading allows animation loops to sleep for a second between each frame
without causing the whole system to pause.

• When a thread blocks in a Java program, only the single thread that is blocked pauses.
All other threads continue to run.
Multithreading in java
• Java provides built-in support for multithreaded programming.

• A multithreaded program contains two or more parts that can run concurrently. Each
part of such a program is called a thread.

• Thread is a smallest unit of code.

• Thread is also defined as a sub process.

• A Thread sometimes called an execution context or a light weight process.

Uses of Threads:

• Threads are used in designing server side programs to handle multiple clients at a
time.

• Threads are used in games and animations.


Creating a Thread

• Write a class that extends Thread class or implements Runnable


interface this is available in lang package.

• Write public void run () method in that class. This is the method by
default executed by any thread

• Create an object to that class.

• Create a thread and attach it to the object.

• Start running the threads.


Thread Synchronization

• Synchronization in java is the capability to control the


access of multiple threads to any shared resource.

• Java Synchronization is better option where we want to


allow only one thread to access the shared resource and
other threads wait

• A shared resource may be corrupted if it is accessed


simultaneously by multiple threads.

8
Thread Synchronization

Why use Synchronization ?


The synchronization is mainly used to
1. To prevent thread interference.
2. To prevent consistency problem.
Types of Synchronization
There are two types of synchronization
i) Process Synchronization
ii) Thread Synchronization

 Here, we will discuss only thread synchronization.

9
Thread Synchronization

 There are two types of thread synchronization:

1) Mutual Exclusive

1. Synchronized method.

2. Synchronized block.

2) Cooperation (Inter-thread communication in java)

10
Mutual Exclusive

 Mutual Exclusive helps keep threads from interfering with one another while
sharing data. This can be done by two ways in java:

1) by synchronized method

2) by synchronized block
Concept of Lock in Java
• Synchronization is built around an internal entity known as the lock or monitor. Every
object has a lock associated with it. By convention, a thread that needs consistent
access to an object's fields has to acquire the object's lock before accessing them, and
then release the lock when it's done with them.

• From Java package, java.util.concurrent.locks contains several lock implementations.

11
Mutual Exclusive

i) Synchronized method

• If you declare any method as synchronized, it is known as synchronized


method.

• Synchronized method is used to lock an object for any shared resource.

• When a thread invokes a synchronized method, it automatically acquires the


lock for that object and releases it when the thread completes its task.

• Syntax:

12
Mutual Exclusive

ii) Synchronized block


• Synchronized block can be used to perform synchronization on any specific resource
of the method. Suppose you have 50 lines of code in your method, but you want to
synchronize only 5 lines, you can use synchronized block.

• If you put all the codes of the method in the synchronized block, it will work same
as the synchronized method.

Syntax to use synchronized block:

13
Inter-thread communication

• Inter-thread communication or Co-operation is all about allowing


synchronized threads to communicate with each other.

• Cooperation (Inter-thread communication) is a mechanism in which a thread


is paused running in its critical section and another thread is allowed to enter
(or lock) in the same critical section to be executed.

• It is implemented by following methods of Object class:

1) wait()

2) notify()

3) notifyAll()
14
Inter-thread communication

1) wait() method
 Causes current thread to release the lock and wait until either another thread invokes
the notify() method or the notifyAll() method for this object, or a specified amount of
time has elapsed.

 The current thread must own this object's monitor, so it must be called from the
synchronized method only otherwise it will throw exception.

15
Inter-thread communication

2) notify() method

 Wakes up a single thread that is waiting on this object's monitor. If


many threads are waiting on this object, one of them is chosen to be
awakened.

 Syntax: public final void notify()


3) notifyAll() method

 Wakes up all threads that are waiting on this object's monitor.

 Syntax: public final void notifyAll()

16
Inter-thread communication

The process of inter-thread communication

17
Deadlock

 Deadlock in java is a part of multithreading. Deadlock can occur


in a situation when a thread is waiting for an object lock, that is
acquired by another thread and second thread is waiting for an
object lock that is acquired by first thread. Since, both threads
are waiting for each other to release the lock, the condition is
called deadlock.

18

You might also like