Multithreading Basics Flashcards

1
Q

4 conditions for deadlock to occur

A

Mutual exclusion : a thread takes a lock which is mutually exclusive over some resource

Hold and wait : That thread than requests for access to another resource which is mutually exclusive and held by some other thread while it holds the lock for the first resource

No Pre-emption : There is no way thread 1 when asking for resource two can force thread 2 to relinquish its lock. Thread 2 will only release the lock voluntarily and same for thread1 and and its own lock.

Circular wait : Each thread is now waiting for the other thread to release their lock before it can proceed

 a hold a wait is basically a nested synchronization

synchronized(resource1)
{

	dosomethings 
	
	synchronized (resource2)

}

you can remove deadlock in some cases by removing hold and wait

by just removing the nesting if possible

void method{
synchronize(resource1)
{}
synchronized(resource2){
}
}

for nested cases you need to look at ordering the acess of resource in the same order .. and by doing that you are removing the circular wait condition

should figure how pre-emption can be used to removed deadlock

if you set an order in which resources are accessed .say give them an integer value and you can only access resources in ascending order .. if you access a resource with lesser number you relinquish the current lock

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

StampedLocks

A

type of lock which return sort of a number value when called
a call to readlock in stamped or reentrantreadwrite tries to update the counter/numeric value possibly its the hold count.. so when you get readlock you are doing an increment which is expensive

stampedlock gives a tryOptimisticRead method which doesnt increment the counter then and before doing compute you check if stamp hasnt changed ( using lock.validate method. ( then what you have read is still current ) more like optimistice CAS ..

if a write has happened then the validate would return false.
in that case you do a blocking read lock as below

interesting to remembeer that a writelock would not be granted if there are current active readlocks .. number of readlocks has to be 0 to be able to grant a writelock .. this applied to ReentrantReadWriteLock

StampedLock lk = new StampedLock();
var ts = lk.tryOptimisticRead();
// do some work.. create object to return using
// fields of this object
var ret = this.firstName + this.lastName // use fields
if(!lk.validate(ts)) { // if a write has occured, then…
ts = lk.readLock(); // acquire readLock (cache unfriendly) and…
try {
// … try do some work again
var ret = this.firstName + this.lastName // use fields
} finally {
lk.unlock(ts);
}

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

when to use ExecutorService

A

Executor Service is used to create a pool of thread to which tasks can be submitted.
Do an Executors.newSingleThreadExecutor
Executors.newFixedThreadPool

creates a pool of one or more threads as per size. limit the number of threads being created

You do a service.submit( any runnable or callable task)

the number of threads stay active till you call service.shutdown.. which then allows all submitted tasks to finish

you can check if service is shutdown by waiting on service.isterminated either forever for a fixed duration using service.awaitTermication( waittime) .

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

common causes of memory leaks
caching
threadlocals

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