Difference between revisions of "Atomicity"

From CSE231 Wiki
Jump to navigation Jump to search
(Created page with "=Motivation= Many race conditions can be prevented by proper encapsulation avoiding check-then-write and read-write-modify patterns. =Background= [https://docs.oracle.com/jav...")
 
Line 6: Line 6:
 
: [https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html Synchronized Methods]
 
: [https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html Synchronized Methods]
 
: [https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html Intrinsic Locks]
 
: [https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html Intrinsic Locks]
 +
 +
=Code To Investigate=
 +
{{CodeToInvestigate|CheckThenActCourse|isSpaceRemaining<br>add<br>drop|atomicity.course.studio}}
 +
 +
<nowiki>public class CheckThenActCourse {
 +
private final Collection<Student> students;
 +
private final int limit;
 +
 +
public CheckThenActCourse(int limit, Supplier<Collection<Student>> collectionSupplier) {
 +
this.students = collectionSupplier.get();
 +
this.limit = limit;
 +
}
 +
 +
public int getLimit() {
 +
return this.limit;
 +
}
 +
 +
public boolean isSpaceRemaining() {
 +
synchronized (this.students) {
 +
return this.students.size() < this.limit;
 +
}
 +
}
 +
 +
public void add(Student student) {
 +
synchronized (this.students) {
 +
this.students.add(student);
 +
}
 +
}
 +
 +
public boolean drop(Student student) {
 +
synchronized (this.students) {
 +
return this.students.remove(student);
 +
}
 +
}
 +
}</nowiki>
 +
 +
=Code To Implement=
 +
{{CodeToImplement|Course|addIfSpace<br>drop|atomicity.course.studio}}
 +
 +
{{Sequential|public boolean addIfSpace(Student student)}}
 +
 +
{{Sequential|public boolean drop(Student student)}}
 +
 +
=Testing Your Solution=
 +
==Correctness==
 +
{{TestSuite|AtomicityTestSuite|atomicity}}

Revision as of 16:52, 12 April 2018

Motivation

Many race conditions can be prevented by proper encapsulation avoiding check-then-write and read-write-modify patterns.

Background

Concurrency Tutorial

Synchronized Methods
Intrinsic Locks

Code To Investigate

class: CheckThenActCourse.java DEMO: Java.png
methods: isSpaceRemaining
add
drop
package: atomicity.course.studio
source folder: src//java
public class CheckThenActCourse {
	private final Collection<Student> students;
	private final int limit;

	public CheckThenActCourse(int limit, Supplier<Collection<Student>> collectionSupplier) {
		this.students = collectionSupplier.get();
		this.limit = limit;
	}

	public int getLimit() {
		return this.limit;
	}

	public boolean isSpaceRemaining() {
		synchronized (this.students) {
			return this.students.size() < this.limit;
		}
	}

	public void add(Student student) {
		synchronized (this.students) {
			this.students.add(student);
		}
	}

	public boolean drop(Student student) {
		synchronized (this.students) {
			return this.students.remove(student);
		}
	}
}

Code To Implement

class: Course.java Java.png
methods: addIfSpace
drop
package: atomicity.course.studio
source folder: student/src/main/java

method: public boolean addIfSpace(Student student) Sequential.svg (sequential implementation only)

method: public boolean drop(Student student) Sequential.svg (sequential implementation only)

Testing Your Solution

Correctness

class: AtomicityTestSuite.java Junit.png
package: atomicity
source folder: testing/src/test/java