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

Lecture 18 Synchronization

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 18 Synchronization

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Maharaja Agrasen Institute of Technology

CIC-212 Java Programming

Synchronization
Synchronization
What if, the threads wants to access the shared resources ?

 There must be some way so that not more than one


thread will be able to access the shared resource. This
can be achieved with synchronization.
 Key to synchronization is the concept of
monitor/semaphore. It is an object which is used as a
mutually exclusive lock or mutex.
 Only one thread can own a monitor at a given time.
When one thread owns a monitor, others have to wait
for that resource.
 The simplest way to use synchronization is by declaring
one or more methods to be synchronized.
Synchronization

 Synchronization prevent data corruption.


 Synchronization allows only one thread to perform
an operation on a object at a time.
 If multiple threads require an access to an object,
synchronization helps in maintaining consistency.
Example
public class Counter{
private int count = 0; In this example, the counter tells
public int getCount() how many access has been made.
{ return count;
If a thread is accessing setCount
}
and updating count and another
thread is accessing getCount at
public setCount( )
the same time, there will be
{ inconsistency in the value of
count++; count.
}
}
Fixing the example
public class Counter{
By adding the synchronized keyword
private static int count = 0;
we make sure that when one thread
public synchronized int is in the setCount method the other
getCount() threads are all in waiting state.
{
The synchronized keyword places a
return count; lock on the object, and hence locks
all the other methods which have the
}
keyword synchronized. The lock
public synchoronized does not lock the methods without
setCount(int count) the keyword synchronized and hence
{ this.count = count; they are open to access by other
threads.
}

}
What about static methods?
public class Counter{
In this example the methods are
private int count = 0;
static and hence are associated with
public static synchronized int getCount() the class object and not the instance.
{
Hence the lock is placed on the class
return count;
object that is, Counter.class object
} and not on the object itself. Any
public static other non static synchronized
synchronized methods are still available for access
setCount(int by other threads.
count)
{
this.count =
count;
}
}
Common Synchronization mistake
public class Counter{
The common mistake here is one
private int count = 0; method is static synchronized
public static synchronized int getCount() and another method is non static
synchronized.
{
return count; This makes a difference as locks
} are placed on two different
objects. The class object and the
public
instance and hence two different
synchroniz threads can access the methods
ed simultaneously.
setCount(int
count)
{
this.count =
count;
}
Synchronization Block
class Caller implements Runnable
class Callme
{
{ String msg; Callme target;
void call(String msg) Thread t;
public Caller(Callme tar,
{ S.O.P(“[“ + msg); String s)
{
try
target=tar;
{ Thread.sleep(1000); msg=s;
t=new Thread(this);
}catch(IE e) {}
t.start();
S.O.P (“]”); }
public void run()
}
{
target.call(ms
} g);
}
}
Synchronization Block
class Sync
{ P.S.V.M (-)
OUTPUT:
{ Callme target=new Callme();
Caller ob1=new Caller(target,”Hello”); [Hello[Sync[World]
]
Caller ob1=new Caller(target,”Sync”); ]
Caller ob1=new Caller(target,”World”);
try
{ ob1.t.join(); ob2.t.join();
ob3.t.join();
}catch(IE e){}
}
}
Synchronization Block
class Callme class Caller implements Runnable
{
{ String msg; Callme target;
void synchronized call(String msg) Thread t;
public Caller(Callme tar,
{ S.O.P(“[“ + msg); String s)
try {
target=tar;
{ Thread.sleep(1000); msg=s;
}catch(IE e) {} t=new Thread(this);
t.start();
S.O.P (“]”); }
} public void run()
{
target.call(ms
} g);
}
}
Synchronization Block
class Caller implements Runnable
class Callme {
String msg; Callme target;
{
Thread t;
void call(String msg) public Caller(Callme tar, String s)
{
{ S.O.P(“[“ + msg);
target=tar;
try msg=s;
t=new Thread(this);
{ Thread.sleep(1000);
t.start();
}catch(IE e) {} }
public void run()
S.O.P (“]”);
{
} synchronized(target)
{ target.call(msg); }
} }
}
Interthread Communication
The wait(), notify(), notifyAll() are used for interthread
Methods
communication. These are functions of Object class.
These method are implemented as final in Object. All three method
can be called only from within a synchronized context.

•wait() tells calling thread to give up monitor and go to sleep until


some other thread enters the same monitor and call notify.

•notify() wakes up a thread that called wait() on same object.

•notifyAll wakes up all the thread that called wait() on same


object. ()
Interthread Communication
wait() sleep()
called from synchronised no such requirement
block
monitor is released monitor is not released
awake when notify() or not awake when notify() or
notifyAll() method is called. notifyAll() method is called
not a static method static method
wait() is generaly used on sleep() method is simply used
condition to put your thread on sleep.

You might also like