Discussion 11 handout

Group members (names & NetIDs)

Objectives

Orientation

Download the dis11 project code and open it as a project in IntelliJ as you would an assignment. Read BarrierDemo, then run it. Is the requirement regarding subtask B being respected?

Unsynchronized attempt

Attempt to implement Barrier::await() without using synchronization (e.g. by “spinning”—checking for a condition in a loop to wait until it becomes true). Ignore postAction for now.

Run BarrierDemo again. What behavior do you see? (different groups may see different behavior)

Synchronization

Where is synchronization needed?

Synchronization is not required everywhere, even when a class is shared between threads. It is important to recognize in which situations synchronization is required, so you can confidently avoid race conditions without unnecessary overhead.

Is synchronization required in the constructor? If so, which object should be synchronized on? If not, justify why not.

Is synchronization required in totalThreads()? If so, which object should be synchronized on? If not, justify why not.

Is synchronization required in awaitingThreads()? If so, which object should be synchronized on? If not, justify why not.

Await

Fix your implementation of await() so that it uses synchronization to avoid race conditions and to block without “spinning”. Continue ignoring postAction for now.

Run BarrierDemo again. Is the requirement regarding subtask B being respected?

postAction

Implement the requirement related to postAction. Run BarrierDemo again. Is the behavior what you would expect?

Reference the state diagram from lecture 20. Which state is each thread in when postAction is executed? Use the portion numbers from your most recent run.

Going further

What would happen if more than totalThreads called await() on a Barrier?