Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment done #72

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,35 @@ public class SynchronizedQueueImpl extends SynchronizedQueue {
// TODO - change this to true if you want to see diagnostic
// output on the console as the test runs.
static {
diagnosticsEnabled = false;
diagnosticsEnabled = true;
}

protected void createThreads() {
// TODO - replace the "null" assignments below to create two
// Java Threads, one that's passed the mProducerRunnable and
// the other that's passed the mConsumerRunnable.
mConsumer = null;
mProducer = null;
mConsumer = new Thread(mConsumerRunnable);
mProducer = new Thread(mProducerRunnable);
}

protected void startThreads() {
// TODO - you fill in here to start the threads. More
// interesting results will occur if you start the
// consumer first.
mConsumer.start();
mProducer.start();
}

protected void interruptThreads() {
// TODO - you fill in here to interrupt the threads.
mConsumer.interrupt();
mProducer.interrupt();
}

protected void joinThreads() throws InterruptedException {
// TODO - you fill in here to wait for the threads to
// exit.
mConsumer.join();
mProducer.join();
}
}
2 changes: 1 addition & 1 deletion assignments/week-2-assignment-1/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Import the necessary Java synchronization and scheduling classes.

package edu.vuum.mocca;

import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand All @@ -18,63 +16,107 @@ class SimpleAtomicLong
* The value that's manipulated atomically via the methods.
*/
private long mValue;



/**
* The ReentrantReadWriteLock used to serialize access to mValue.
*/
// TODO - add the implementation

// TODO -- you fill in here by replacing the null with an
// initialization of ReentrantReadWriteLock.
private ReentrantReadWriteLock mRWLock = new ReentrantReadWriteLock();

/**
* Creates a new SimpleAtomicLong with the given initial value.
*/
public SimpleAtomicLong(long initialValue) {
// TODO - you fill in here
public SimpleAtomicLong(long initialValue)
{
// TODO -- you fill in here
this.mValue = initialValue;
}

/**
* @brief Gets the current value
* @brief Gets the current value.
*
* @returns The current value
*/
public long get() {
// TODO - you fill in here
public long get()
{
long value;

// TODO -- you fill in here
mRWLock.readLock().lock();
value = this.mValue;
mRWLock.readLock().unlock();

return value;
}

/**
* @brief Atomically decrements by one the current value
*
* @returns the updated value
*/
public long decrementAndGet() {
// TODO - you fill in here
public long decrementAndGet()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = --mValue;
mRWLock.writeLock().unlock();

return value;
}

/**
* @brief Atomically increments by one the current value
*
* @returns the previous value
*/
public long getAndIncrement() {
// TODO - you fill in here
public long getAndIncrement()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = mValue++;
mRWLock.writeLock().unlock();

return value;
}

/**
* @brief Atomically decrements by one the current value
*
* @returns the previous value
*/
public long getAndDecrement() {
// TODO - you fill in here
public long getAndDecrement()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = mValue--;
mRWLock.writeLock().unlock();

return value;
}

/**
* @brief Atomically increments by one the current value
*
* @returns the updated value
*/
public long incrementAndGet() {
// TODO - you fill in here
public long incrementAndGet()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = ++mValue;
mRWLock.writeLock().unlock();

return value;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ public class SimpleAtomicLongMultithreadedTest {
static CountDownLatch mStopLatch;

/**
* An instance of our implementation of SimpleAtomicLong, which is
* defined as "volatile" to ensure proper visibility of its fields
* after construction.
* An instance of our implementation of SimpleAtomicLong.
*/
static volatile SimpleAtomicLong mCounter;
static SimpleAtomicLong mCounter;

/**
* Runnable commands that use the mCounter methods
Expand Down
16 changes: 7 additions & 9 deletions assignments/week-3-assignment-2/Assignment-Description.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Released Monday, May 26th, 2014
Due Monday, June 9th, 2014

In this assignment, you will use a Java ReentrantLock and Java
Condition to implement a subset of the Java
ConditionObject to implement a subset of the Java
java.util.concurrent.Semaphore class, which we call SimpleSemaphore.
This assignment also reuses the SimpleAtomicLong you implemented for
This assignment also reuses the SimpleAtomicLock you implemented for
week-2-assignment-1, so make sure it's compiling and running properly
before attempting this assignment!

Expand All @@ -19,7 +19,7 @@ Palantirs if you're not yet a fan of Tolkein's Lord of the Ring's.
The PalantirManagerUnitTest.java program creates three Palantiri and
five Threads (one for each Palantir user) that concurrently attempt to
acquire a Palantir and gaze into it for a certain amount of time. If
the SimpleSemaphore and SimpleAtomicLong are implemented properly the
the SimpleSemaphore and SimpleAtomicLock are implemented properly the
test should succeed without throwing any exceptions, as described
further below.

Expand All @@ -36,14 +36,12 @@ In particular, you'll need to do the following:

. Implement the SimpleAtomicLong class, which you should replace with
your solution to week-2-assignment-1, after applying any fixes
motivated by watching the Virtual Office Hours video of the
instructor's solution(s). This class is only used by the
suggested by peer graders. This class is only used by the
PalantirManagerUnitTest.java and should not be used in the
SimpleSemaphore implementation itself.

. Implement the SimpleSemaphore class using a Java ConditionObject
(accessed via a Condition) and Java ReentrantLock, which are covered
in these videos:
. Implement the SimpleSemaphore class using a Java ConditionObject and
Java ReentrantLock, which are covered in these videos:

Section 1: Module 2: Part 5: Java ReentrantLock
Section 1: Module 2: Part 8: Java ConditionObject
Expand Down Expand Up @@ -91,7 +89,7 @@ should disappear!

Right click on the test suite (AllTests.java) or an individual
*_UnitTest.java file in Eclipse and select 'Run As' -> 'JUnit
Test'. When the assignment is complete, all the tests should complete
Test'. When the assignment is complete, 12 of 12 tests should complete
successfully. If a test passes a green-check mark will appear next to
the test in the JUnit view. As long as this JUnit test "passes"
successfully your program will be be consider "correct" for the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Import the necessary Java synchronization and scheduling classes.

package edu.vuum.mocca;

import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand All @@ -18,63 +16,107 @@ class SimpleAtomicLong
* The value that's manipulated atomically via the methods.
*/
private long mValue;



/**
* The ReentrantReadWriteLock used to serialize access to mValue.
*/
// TODO - add the implementation

// TODO -- you fill in here by replacing the null with an
// initialization of ReentrantReadWriteLock.
private ReentrantReadWriteLock mRWLock = new ReentrantReadWriteLock();

/**
* Creates a new SimpleAtomicLong with the given initial value.
*/
public SimpleAtomicLong(long initialValue) {
// TODO - you fill in here
public SimpleAtomicLong(long initialValue)
{
// TODO -- you fill in here
this.mValue = initialValue;
}

/**
* @brief Gets the current value
* @brief Gets the current value.
*
* @returns The current value
*/
public long get() {
// TODO - you fill in here
public long get()
{
long value;

// TODO -- you fill in here
mRWLock.readLock().lock();
value = this.mValue;
mRWLock.readLock().unlock();

return value;
}

/**
* @brief Atomically decrements by one the current value
*
* @returns the updated value
*/
public long decrementAndGet() {
// TODO - you fill in here
public long decrementAndGet()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = --mValue;
mRWLock.writeLock().unlock();

return value;
}

/**
* @brief Atomically increments by one the current value
*
* @returns the previous value
*/
public long getAndIncrement() {
// TODO - you fill in here
public long getAndIncrement()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = mValue++;
mRWLock.writeLock().unlock();

return value;
}

/**
* @brief Atomically decrements by one the current value
*
* @returns the previous value
*/
public long getAndDecrement() {
// TODO - you fill in here
public long getAndDecrement()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = mValue--;
mRWLock.writeLock().unlock();

return value;
}

/**
* @brief Atomically increments by one the current value
*
* @returns the updated value
*/
public long incrementAndGet() {
// TODO - you fill in here
public long incrementAndGet()
{
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
value = ++mValue;
mRWLock.writeLock().unlock();

return value;
}
}

Loading