Bpq,utq,ulfq (topic 9) Flashcards

1
Q

what does bounded mean

A

fixed capacity

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

what does total mean?

A

a method is total if calls do not wait for conditions to become try

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

what does partial mean?

A

A method may wait for conditions to hold (suspending)

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

What does synchronous mean?

A

a method is synchronous if it waits for another method to overlap its call interval(A method call that adds an item to the pool is blocked until that item is removed by another method call)

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

Blocking vs Non-Blocking

A

Blocking: caller waits until state changes
Non-Blocking: Method throws exception

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

Does the enq and deq operate on the same sides of the queue?

A

no. so enq + deq should be able to proceed concurrently without interference. only concurrent calls will interfere

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

Properties of a queue(what is it and what does it consist of):

A

queue = linked list
queue has head and tail fields that refer to 1st and last nodes in list
queue contains a sentinel node (place-holder)

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

What are the 2 locks used(BQ)?

A

-deqlock (lock the front of the queue to ensure that there is not 1+ dequeuer at a time
-enqlock (lock end of queue to ensure that there is not more than one enqueuer at a time)

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

perks of 2 locks

A

enqueuers and dequeuers can operate independently

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

can enq calls continue while list is full and deq calls when list is empty?

A

No

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

What does the conditions do in the BPQ

A

notFullCondition = notify enqueuers when list is not full anymore
notEMptyCondition = notify dequeuers when list is not empty anymore

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

How do we keep track of the number of available slots in the BQ?

A

use a size field that is an AtomicInteger

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

code for the BoundedQueue class

A

public class BoundedQueue<T> {
ReentrantLock enqLock, deqLock;
Condition notEmptyCondition, notFullCondition;
AtomicInteger size;
Node head;
Node tail;
int capacity;
enqLock = new ReentrantLock();
notFullCondition = enqLock.newCondition();
deqLock = new ReentrantLock();
notEmptyCondition = deqLock.newCondition();
}</T>

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

enq code for bounded queue

A

public void enq(T x) {
boolean mustWakeDequeuers = false;
enqLock.lock();
try {
while (size.get() == capacity)
notFullCondition.await();
Node e = new Node(x);
tail.next = e;
tail = tail.next;
if (size.getAndIncrement() == 0)
mustWakeDequeuers = true;
} finally {
enqLock.unlock();
}
if (mustWakeDequeuers)
notEmptyCondition.signalAll();
}
}
}

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

deq code for bounded queue

A

public T deq() {
T result;
boolean mustWakeEnqueuers = false;
deqLock.lock();
try {
while (size.get() == 0)
notEmptyCondition.await();
result = head.next.value;
head = head.next;
if (size.getAndDecrement() == capacity)
mustWakeEnqueuers = true;
} finally {
deqLock.unlock();
}
if (mustWakeEnqueuers) {
notFullCondition.signalAll();
}
return result;
}}

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

Bad thing about deq + enq methods in BPQ

A

share atomic counter which is accessed on every method call

17
Q

Solution with 2 counters in BPQ

A

enq: checks enqSideSize and proceeds as long as less than capacity
deq: checks deqSideSize and proceeds as long as greater than zero

18
Q

UTQ enq:

A

public void enq(T x) {
enqLock.lock();
try {
Node e = new Node(x);
tail.next = e;
tail = tail.next;
} finally {
enqLock.unlock();
}
}

19
Q

UTQ deq

A

public T deq() throws EmptyException {
T result;
deqLock.lock();
try {
if (head.next == null)
throw new EmptyException();
result = head.next.value;
head = head.next;
} finally {
deqLock.unlock();
}
return result;
}

20
Q

ULFQ idea:

A

extension of unbounded queue.
prevents method calls from starving by having quicker threads help slower threads

21
Q

ULFQ structure:

A

linked list. each nodes next = atomicReference (for lockfree operations)

22
Q

LFQ node class

A

public class Node {
public T value;
public AtomicReference<Node> next;</Node>

public Node(T value) {
value = value;
next = new AtomicReference<Node>(null);
}
}</Node>

23
Q

What are the 2 enqueue steps of the LFQ?

A

Logical enqueue - appends node to linked list
Physical enqueue - tail field to point to new node

24
Q

are there 2 enqueue steps of the LFQ atomic?

A

no

25
Q

What should you do if you find tail pointing to the wrong value in LFQ?

A

If tail node has non-null next field CAS the queues tail field to tail.next

26
Q

ULFQ enq:

A

public void enq(T value) {
Node node = new Node(value);
while (true) {
Node last = tail.get();
Node next = tail.next.get();
if (last == tail.get()) {
if (next == null) {
if (last.next.compareAndSet(next, node)) {
tail.compareAndSet(last, node);
return;
} else
tail.compareAndSet(last, last.next);
}
}
}
}

27
Q

ULFQ deq:

A

public T deq() throws EmptyException {
while (true) {
Node first = head.get();
Node last = tail.get();
Node next = first.next.get();
if (first == head.get()) {
if (first == last) {
if (next == null) {
throw new EmptyException();
}
tail.compareAndSet(last, next);
} else {
T value = next.value;
if (head.compareAndSet(first, next))
return value;
}
}}}