Java Synchronization Flashcards

1
Q

Why we need Java synchronization ?

A

When there is two or more threads, they might use same resource ,for example, if multiple threads try to write within the same file , they may corrupt data , one of threads can override data or while one thread try to open a file at the same time another thread may close it, so it produce unforeseen results.
So , there is need to synchronize action of multiple threads and make sure only one thread can access the resource at a given point in time.This is implemented using a concept called monitors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is monitor concept ?

A

Each object in java is associated with a monitor , which a thread can lock or unlock. Only one thread at a time can hold a lock on a monitor .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

synchronized

A

used to synchronize java thread tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

synchronized Syntax

A

synchronized(objectRef){

}

=> objectRef is a reference to an object associated with a monitor (which only one thread can hold a lock at a time)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Example of synchronized use

A

We write two threads for a counter program
=> Without synchronized() the count is not in sequence but when we write print counter method inside synchronized(PrintCounterRef){
PrintCounterMethod()} the count is in sequence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Static Synchronization

A

when we use this type of synchronization , the thread on synchronization region have access and all other threads are blocked until the thread allowed ends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Syntax of Static Synchronization

A

static synchronized returnType nameOfMethod( Type parameters) {
// code
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

inter-thread communication

A

happens when threads exchange information

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to achieve inter-thread communication ?

A

we use this methods:
*wait(): a thread wait until another thread invoke notify().
*notify():Wakes up a single thread waiting on this object’s monitor
*notifyAll(): Wakes up all threads that called wait() on the same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Reentrant Monitor(ReentrantLock)

A

is mechanism for synchronization that allows same thread acquire same lock multiple times without causing deadLock.
=> It is one of deadLock solutions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How Reentrant Monitor works ?

A

*Reentrancy: a monitor is re-entrant when its thread can re-enter same synchronized block or method it already holds lock on
*Hold count : when a thread acquire a reentrant lock it increments hold count , same thing happens when it acquires the same lock again and opposite happen when it release the lock.Hold count is set to 0 when a lock is fully released

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Reentrant Monitor Benefits

A

*Avoids DeadLock
*More flexible than synchronized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly