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

Creating latest pull #92

Open
wants to merge 7 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 @@ -220,21 +220,27 @@ public static SynchronizedQueueResult testQueue(QueueAdapter<Integer> queue) {
// initialization below to create two Java Threads, one
// that's passed the producerRunnable and the other that's
// passed the consumerRunnable.
Thread consumer = null;
Thread producer = null;
Thread consumer = new Thread(consumerRunnable) ;
Thread producer = new Thread(producerRunnable);

// TODO - you fill in here to start the threads. More
// interesting results will occur if you start the
// consumer first.

producer.start();
consumer.start();

// Give the Threads a chance to run before interrupting
// them.
Thread.sleep(100);

// TODO - you fill in here to interrupt the threads.
producer.interrupt();
consumer.interrupt();

// TODO - you fill in here to wait for the threads to
// exit.
producer.join();
consumer.join();

// Do some sanity checking to see if the Threads work as
// expected.
Expand Down
182 changes: 114 additions & 68 deletions assignments/week-2-assignment-1/src/edu/vuum/mocca/SimpleAtomicLong.java
Original file line number Diff line number Diff line change
@@ -1,80 +1,126 @@
// Import the necessary Java synchronization and scheduling classes.

package edu.vuum.mocca;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.Lock;

/**
* @class SimpleAtomicLong
*
*
* @brief This class implements a subset of the
* java.util.concurrent.atomic.SimpleAtomicLong class using a
* ReentrantReadWriteLock to illustrate how they work.
*/
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

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

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

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

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

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

/**
* @brief Atomically increments by one the current value
*
* @returns the updated value
*/
public long incrementAndGet() {
// TODO - you fill in here
}
}
class SimpleAtomicLong {
/**
* The value that's manipulated atomically via the methods.
*/
private long mValue;

/**
* The ReentrantReadWriteLock used to serialize access to mValue.
*/

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

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

/**
* @brief Gets the current value.
*
* @returns The current value
*/

public long get() {
long value;

// TODO -- you fill in here
mRWLock.readLock().lock();
try {
value = mValue;
} finally {
mRWLock.readLock().unlock();
}
return value;
}

/**
* @brief Atomically decrements by one the current value
*
* @returns the updated value
*/
public long decrementAndGet() {
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
try {
value = --mValue;
} finally {
mRWLock.writeLock().unlock();
}
return value;
}

/**
* @brief Atomically increments by one the current value
*
* @returns the previous value
*/
public long getAndIncrement() {
long value = 0;

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

return value;
}

/**
* @brief Atomically decrements by one the current value
*
* @returns the previous value
*/
public long getAndDecrement() {
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
try {
value = mValue--;
} finally {
mRWLock.writeLock().unlock();
}
return value;
}

/**
* @brief Atomically increments by one the current value
*
* @returns the updated value
*/
public long incrementAndGet() {
long value = 0;

// TODO -- you fill in here
mRWLock.writeLock().lock();
try {
value = ++mValue;
} finally {
mRWLock.writeLock().unlock();
}
return value;
}
}
Loading