[go: up one dir, main page]

0% found this document useful (0 votes)
30 views13 pages

16 Multithreading

Threads need synchronization mechanisms to safely access shared objects and prevent race conditions. Synchronization can be achieved through synchronized methods or blocks. A monitor is used for mutual exclusion, allowing only one thread to own the monitor at a time. Threads enter the monitor by executing a synchronized method or block on an object, and wait to enter if the monitor is currently locked by another thread.

Uploaded by

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

16 Multithreading

Threads need synchronization mechanisms to safely access shared objects and prevent race conditions. Synchronization can be achieved through synchronized methods or blocks. A monitor is used for mutual exclusion, allowing only one thread to own the monitor at a time. Threads enter the monitor by executing a synchronized method or block on an object, and wait to enter if the monitor is currently locked by another thread.

Uploaded by

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

1

Multi threading cont.


2
Synchronization
Many times, 2 or more threads need to share access
to the same objects.
When this happens, they need some way to ensure
that the object will be modified by only one thread at
a time.
Else, the threads enter into a race condition

3
Synchronization
consider a deposit operation:

public void deposit(Account a, float amount)
{
float temp;
temp = a.bal;
temp+=amount;
a.bal = temp;
}

4
Thread Synchronization
Key to synchronization is the concept of the
monitor.
A monitor is an object that is used as a mutually
exclusive lock.
Only one thread can own a monitor at a given
time.
When a thread acquires a lock, it is said to have
entered the monitor.
All other threads attempting to enter the locked
monitor will be suspended until the first thread
exits the monitor.


5
Thread Synchronization

Thread synchronization can be achieved in two
ways -
Synchronized methods
Synchronized block

6
synchronization
A thread becomes the owner of the object's
monitor in one of two ways:
By executing a synchronized instance method
of that object.
By executing the body of a synchronized
statement that synchronizes on the object.
Only one thread at a time can own an object's
monitor.

7
synchronized method
public synchronized void deposit(Account a, float
amount)
{
float temp;
temp = a.bal;
temp+=amount;
a.bal = temp;
}

8
synchronized blocks
synchronized blocks allow for activity-centered
Synchronization.
Lock of an object is acquired prior to entry into block &
released upon exit from block.
public void printValue(Point p){
// variable declaration
synchronized(p){
// code
}
// more code of the printValue method
}
9
Thread Synchronization
Periodically the thread scheduler activates the
threads that are waiting for the key.
When one of the threads waiting to use the object
runs again, it checks if object is still locked.
All threads are still free to call unsynchronized
methods on the locked object.
If a thread exits a synchronized method by
throwing an exception, it still relinquishes the
object lock.
10
wait()
Method of the Object super class (not class
Thread).
Allows thread to wait inside a synchronized method.
When invoked, the current thread is blocked & gives
up the object lock.
Waits to be notified by another thread of a change
in this object.
This lets another thread entry into the monitor.
11
Necessity for notify() , notifyAll()
When a thread enters wait(), it has no way of
unblocking itself.
If all threads wait, it leads to DEADLOCKS.
notify() :Wakes up a single thread that is
waiting.
This method should only be called by a thread
that is the owner of the object's monitor.
notifyAll () - Wakes up all threads that are
waiting on this object.
12
Thread Groups
Some programs contain a number of threads.
It would be useful to categorize them by
functionality.
This lets you work simultaneously with a number
of threads.
Class ThreadGroup contains methods for creating
& manipulating groups of threads.
13
Thread Groups example
ThreadGroup tg = new ThreadGroup(TG1);
Thread t1 = new Thread(tg,Thread1);
Thread t2 = new Thread(tg,Thread2);

if (tg.activeCount() != 0)
{
tg.interrupt();
}

You might also like