Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Subject:Concurrency The question is from the book called \"The art of multiproce

ID: 664532 • Letter: S

Question

Subject:Concurrency

The question is from the book called "The art of multiprocessor programming"

chapter 10

Exercise 122. Consider the unbounded lock-based queue’s deq() method in
Fig. 10.8. Is it necessary to hold the lock when checking that the queue is not
empty? Explain

11 public T deq() throws EmptyException {
12 T result;
13 deqLock.lock();
14 try {
15 if (head.next == null) {
16 throw new EmptyException();
17 }
18 result = head.next.value;
19 head = head.next;
20 } finally {
21 deqLock.unlock();
22 }
23 return result;
24 }
Figure 10.8 The UnboundedQueue<T> class: the deq() method.

Explanation / Answer

import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; class Server { public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(9000); while (true) { final Socket s = socket.accept(); Runnable r = new Runnable() { @Override public void run() { doWork(s); } }; new Thread(r).start(); } } static void doWork(Socket s) { } }