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

sss #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

sss #97

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,22 +220,25 @@ 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.

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

// TODO - you fill in here to interrupt the threads.

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

consumer.join();
producer.join();
// Do some sanity checking to see if the Threads work as
// expected.
if (consumer == null
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,124 @@ 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();
try{
value = mValue;
}finally{
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();
try{
mValue = mValue - 1;
value = mValue;
}finally{
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();
try{
value = mValue;
mValue = mValue + 1;
}finally{
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();
try{
value = mValue;
mValue = mValue - 1;
}finally{
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();
try{
mValue = mValue + 1;
value = mValue;
}finally{
mRWLock.writeLock().unlock();
}

return value;
}
}

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,124 @@ 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();
try{
value = mValue;
}finally{
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();
try{
mValue = mValue - 1;
value = mValue;
}finally{
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();
try{
value = mValue;
mValue = mValue + 1;
}finally{
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();
try{
value = mValue;
mValue = mValue - 1;
}finally{
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();
try{
mValue = mValue + 1;
value = mValue;
}finally{
mRWLock.writeLock().unlock();
}

return value;
}
}

Loading