CMSC 132 Quiz 5 Worksheet: Threading and Networking, Quizzes of Computer Science

Information about quiz 5 for cmsc 132, including the quiz date, format, and covered topics. The topics include threading in java, such as creating threads, thread advantages and disadvantages, and thread synchronization. Networking topics include tcp and udp, their reliability, and merits for specific applications. The document also includes exercises to help students prepare for the quiz.

Typology: Quizzes

Pre 2010

Uploaded on 07/30/2009

koofers-user-gz1
koofers-user-gz1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132 Quiz 5 Worksheet
The next quiz of the course will be on Wednesday, May 2 during your lab (discussion) session. The
following list provides more information about the quiz:
The quiz will be a written quiz (no computer).
Closed book, closed notes quiz.
Answers must be neat and legible. We recommend that you use pencil and eraser.
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
1.
What are two advantages to multi-threading?
2.
What are two disadvantages to using multi-threading?
3.
What are two ways to create threads in Java?
4.
What is a daemon thread?
5.
What is a deadlock? How can you avoid it?
6.
What is a data race? How can you avoid it?
7.
Give an example of a Java code with a data race.
a.
Eliminate the data race using synchronized methods, e.g., synchronized foo( ) { … }
b.
Eliminate the data race using synchronized objects, e.g., synchronized(bar) { … }
8.
Modify the following class so we can create threads that print messages. For the modified class,
provide a main method that creates and starts t wo 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);
}
}
9.
The following class implements a queue.
public class MyQueue<E> {
private ArrayList<E> list = new ArrayList<E>();
public boolean isEmpty() {
synchronized(this) {
if (list.size() == 0)
return true;
return false;
}
}
pf2

Partial preview of the text

Download CMSC 132 Quiz 5 Worksheet: Threading and Networking and more Quizzes Computer Science in PDF only on Docsity!

CMSC 132 Quiz 5 Worksheet

The next quiz of the course will be on Wednesday, May 2 during your lab (discussion) session. The following list provides more information about the quiz:

  • The quiz will be a written quiz (no computer).
  • Closed book, closed notes quiz.
  • Answers must be neat and legible. We recommend that you use pencil and eraser.

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

1. What are two advantages to multi-threading?

2. What are two disadvantages to using multi-threading?

3. What are two ways to create threads in Java?

4. What is a daemon thread?

5. What is a deadlock? How can you avoid it?

6. What is a data race? How can you avoid it?

7. Give an example of a Java code with a data race.

a. Eliminate the data race using synchronized methods, e.g., synchronized foo( ) { … }

b. Eliminate the data race using synchronized objects, e.g., synchronized(bar) { … }

8. Modify the following class so we can create threads that print messages. For the modified class,

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); }

9. The following class implements a queue.

public class MyQueue { private ArrayList list = new ArrayList();

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.

  • Otherwise, if queue is empty, return null */ public E dequeue() { if (!isEmpty()) return getFirst(); return null; } }

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).

Networking

10. TCP is unreliable. True/False

11. UDP has low overhead. True/False.

12. Say you have two computers on the network (machines A and B). Machine A needs to send a

short, 4-byte message to machine B, and doesn’t expect to see any message in response. Discuss the merits of using TCP vs. UDP for this communication.

13. Which of the following applications can use UDP? Circle those that apply.

a. Video Streaming

b. Online banking

c. ftp client

14. What information do we need in addition to the IP address in order to connect to a server?

15. What is a DNS server? What is a DHCP server? What is NAT?

16. What is the difference between the Java Socket and ServerSocket classes?

17. What is the task performed by the accept method of the Java ServerSocket class?

18. Say a server has obtained a socket to communicate with a client (e.g., clientRequestSocket =

serverSocket.accept()). What should the server do next in order to send and receive information from the client? You do not need to write code, just indicate what the server should do.