1
h07
CS56 S19
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
4pm,5pm,6pm
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h07: HFJ 15 and 16: Multithreads and Collections

ready? assigned due points
true Mon 05/27 05:00PM Mon 06/03 05:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)


Reading Assignment:

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts (4pm,5pm,6pm) in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  2. Threads can be constructed with objects implementing the Runnable interface.

    1. (5 pts) What method definition is necessary when implementing the Runnable interface? Write the entire method signature (return type, name, and parameters (if any)).

    2. (10 pts) When a thread is created, what role does the method stated in part a. above have?

  3. After a Java thread is created, the JVM thread scheduler categorizes the threads in two states throughout execution.

    1. (10 pts) In your own words, briefly describe the two states of a running Java thread.

    2. (10 pts) On pg 496, the book states several ways a thread will be moved to a “blocked” state. State two ways this may happen.

    3. (5 pts) When a thread wakes up from sleeping, which state mentioned in part a. above does it move to?

  4. (10 pts) How do multiple threads behave when dealing with multiple synchronized methods (describe this using the lock / key analogy used on pg 511)?

  5. (5 pts) The book mentions making the makeWithdrawal() method in the Ryan and Monica example as one “atomic” thing. What does the book mean when it uses the term “atomic”?

  6. (10 pts) HFJ Chapter 16 describes several different Java collections. Briefly describe the difference between a HashMap and a LinkedHashMap.

  7. (10 pts) The java.util.Collections (note: this is different than the java.util.Collection interface) has a static sort method.

    1. (5 pts) The Collections.sort method signature shown on pg 539 shows “T extends Comparable” - in your own words, briefly describe what this means.

    2. (5 pts) A compilation error is shown on pg 538 when compiling the example on pg 535 - 537. Why does this code not compile?

    3. (5 pts) What method would need to be added to the Song class in order for this example to compile correctly?