

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CMSC 132 Quiz 4 Worksheet
The next quiz of the course will be on Friday, July 27. The following list provides more information about the quiz:
The following exercises cover the material to be included in this quiz. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with TAs and instructors during office hours.
Thread/Synchronization
provide a main method that creates and starts two threads, one printing the message “Testudo” and the other the message “Terps”. A third thread displaying “UMCP” will be created and started only after the previous two threads and the main thread have finished.
public class PrtMessage { private String message;
public PrtMessage(String message) { this.message = message; }
public void print() { for (int i=0; i<50; i++) System. out .println(message); }
public class MyQueue
public boolean isEmpty() { synchronized(this) { if (list.size() == 0) return true; return false; } }
public E getFirst() { synchronized(this) { return list.remove(0); // removes first element and shift // elements to the right } }
public void offer(E data) { synchronized(this) { list.add(data); } } /** If queue not empty, remove value from queue and return it.
i. Describe a possible scenario where the dequeue method will not work as documented when the dequeue is accessed by multiple threads.
ii. Modify the offer and dequeue methods in order to allow the dequeue method to wait until it can return a value when the queue is empty it (as opposed to returning null).