Upload
rolf-harrell
View
222
Download
0
Embed Size (px)
Citation preview
Dr. R R Manza @ DOCSIT, Dr BAMU
Basic Java :Multi Threading Cont.2
Objectives of This Session
• Explain Synchronization in threads
• Demonstrate use of synchronization using Bank Account example
• Explain & demonstrate wait (), notify () & notifyAll().
• Identify the need for thread Groups.
Basic Java :Multi Threading Cont.3
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”
Basic Java :Multi Threading Cont.4
Synchronization
consider a deposit operation:
public void deposit(Account a, float amount){
float temp;temp = a.bal;temp+=amount;a.bal = temp;
}
Basic Java :Multi Threading Cont.5
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.
Basic Java :Multi Threading Cont.6
Thread Synchronization
• Thread synchronization can be achieved in two ways -
Synchronized methods Synchronized block
Basic Java :Multi Threading Cont.7
Synchronization
• A thread becomes the owner of the object's monitor in one of three 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.
Basic Java :Multi Threading Cont.8
Synchronized Method
public synchronized void deposit(Account a, float amount){
float temp;temp = a.bal;temp+=amount;a.bal = temp;
}
Basic Java :Multi Threading Cont.9
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 declarationsynchronized(p){
// code}
// more code of the printValue method}
Basic Java :Multi Threading Cont.10
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.
Basic Java :Multi Threading Cont.11
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.
Basic Java :Multi Threading Cont.12
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.
Basic Java :Multi Threading Cont.13
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.
Basic Java :Multi Threading Cont.14
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();}