Lazy synchronization, Optimistic synchronization Flashcards

1
Q

What is the main idea behind lazy synchronization?

A

Postpone hard work
split 2 removal phases:
logical removal (mark component to be deleted)
physical removal(Do what needs to be done)

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

what call are most likely to be made in Lazy:

A

contains()

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

how does lazy refine optimistic sync

A

contains() are wait-free and add + remove traverse list only once in absence of contention

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

What does each node have extra in lazy?

A

boolean field called marked which indicates whether the node is reachable. Every unmarked node is reachable

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

Does the contains() every lock in lazy?

A

No

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

Does validation require to traverse through the list to determine if a node is reachable?

A

No

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

How does the validate function look in lazy()

A

private boolean
validate(Node pred, Node curr) {
return
!pred.marked &&
!curr.marked &&
pred.next == curr);
}

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

How does the contains method look for lazy?

A

public boolean contains(Item item) {
int key = item.hashCode();
Node curr = this.head;
while (curr.key < key) {
curr = curr.next;
}
return curr.key == key && !curr.marked;
}

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

how does the add method look for lazy?

A

same as optimistic
public boolean add(T item) {
int key = item.hashCode();
while (true) {
Node pred = head;
Node curr = pred.next;
while (curr.key < key) {
pred = curr; curr = curr.next;
}
pred.lock(); curr.lock();
try {
if (validate(pred, curr)) {
if (curr.key == key) {
return false;
} else {
Node node = new Node(item);
node.next = curr;
pred.next = node;
return true;
}}} finally {
pred.unlock(); curr.unlock();
}}}

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

How does remove look for Lazy?

A

public boolean remove(T item) {
int key = item.hashCode();
Node pred = head;
Node curr = pred.next;
while (curr.key < key) {
pred = curr;
curr = curr.next;
}
try {
pred.lock(); curr.lock();
if (validate(pred,curr) {
if (curr.key == key) {
curr.marked = true;
pred.next = curr.next;
return true;
} else {
return false;
}}} finally {
pred.unlock();
curr.unlock();
}}}

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

how does the contains method look for optimistic sync?

A

public boolean contains(T item) {
int key = item.hashCode();
Node pred = head;
Node curr = pred.next;
while (curr.key < key) {
pred = curr; curr = curr.next;
}
try {
pred.lock(); curr.lock();
if (validate(pred, curr))
return (curr.key == key)
} finally {
pred.unlock(); curr.unlock();
}

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

Good + bad about Optimistic sync:

A

Good: less lock get/release = better performance + concurrency
bad: need to traverse list twice + contains still acquires locks

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