Skip to content

Commit c6afd16

Browse files
committed
start code
1 parent c54539a commit c6afd16

File tree

109 files changed

+7978
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+7978
-0
lines changed

.DS_Store

4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>edu.coursera.concurrent</groupId>
6+
<artifactId>miniproject_1</artifactId>
7+
<packaging>jar</packaging>
8+
<version>0.0</version>
9+
<name>miniproject_1</name>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-resources-plugin</artifactId>
19+
<version>2.4.3</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>3.8.2</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<pluginManagement>
31+
<plugins>
32+
<plugin>
33+
<!-- specify the java version to use during compilation -->
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.1</version>
37+
<configuration>
38+
<source>1.8</source>
39+
<target>1.8</target>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<!-- populates the properties for dependency jar paths -->
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-dependency-plugin</artifactId>
46+
<version>2.9</version>
47+
<executions>
48+
<execution>
49+
<goals>
50+
<goal>properties</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
<plugin>
56+
<!-- executes test with -Xmx option -->
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>2.17</version>
60+
<configuration>
61+
<forkMode>pertest</forkMode>
62+
<argLine>-Xmx4g</argLine>
63+
<useSystemClassLoader>true</useSystemClassLoader>
64+
<testFailureIgnore>true</testFailureIgnore>
65+
</configuration>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-checkstyle-plugin</artifactId>
70+
<version>2.17</version>
71+
<executions>
72+
<execution>
73+
<id>checkstyle</id>
74+
<phase>validate</phase>
75+
<configuration>
76+
<configLocation>${basedir}/src/main/resources/checkstyle.xml</configLocation>
77+
<encoding>UTF-8</encoding>
78+
<consoleOutput>true</consoleOutput>
79+
<failsOnError>true</failsOnError>
80+
<failOnViolation>true</failOnViolation>
81+
</configuration>
82+
<goals>
83+
<goal>check</goal>
84+
</goals>
85+
</execution>
86+
</executions>
87+
</plugin>
88+
</plugins>
89+
</pluginManagement>
90+
</build>
91+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package edu.coursera.concurrent;
2+
3+
/**
4+
* Wrapper class for two lock-based concurrent list implementations.
5+
*/
6+
public final class CoarseLists {
7+
/**
8+
* An implementation of the ListSet interface that uses Java locks to
9+
* protect against concurrent accesses.
10+
*
11+
* TODO Implement the add, remove, and contains methods below to support
12+
* correct, concurrent access to this list. Use a Java ReentrantLock object
13+
* to protect against those concurrent accesses. You may refer to
14+
* SyncList.java for help understanding the list management logic, and for
15+
* guidance in understanding where to place lock-based synchronization.
16+
*/
17+
public static final class CoarseList extends ListSet {
18+
/*
19+
* TODO Declare a lock for this class to be used in implementing the
20+
* concurrent add, remove, and contains methods below.
21+
*/
22+
23+
/**
24+
* Default constructor.
25+
*/
26+
public CoarseList() {
27+
super();
28+
}
29+
30+
/**
31+
* {@inheritDoc}
32+
*
33+
* TODO Use a lock to protect against concurrent access.
34+
*/
35+
@Override
36+
boolean add(final Integer object) {
37+
Entry pred = this.head;
38+
Entry curr = pred.next;
39+
40+
while (curr.object.compareTo(object) < 0) {
41+
pred = curr;
42+
curr = curr.next;
43+
}
44+
45+
if (object.equals(curr.object)) {
46+
return false;
47+
} else {
48+
final Entry entry = new Entry(object);
49+
entry.next = curr;
50+
pred.next = entry;
51+
return true;
52+
}
53+
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*
58+
* TODO Use a lock to protect against concurrent access.
59+
*/
60+
@Override
61+
boolean remove(final Integer object) {
62+
Entry pred = this.head;
63+
Entry curr = pred.next;
64+
65+
while (curr.object.compareTo(object) < 0) {
66+
pred = curr;
67+
curr = curr.next;
68+
}
69+
70+
if (object.equals(curr.object)) {
71+
pred.next = curr.next;
72+
return true;
73+
} else {
74+
return false;
75+
}
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*
81+
* TODO Use a lock to protect against concurrent access.
82+
*/
83+
@Override
84+
boolean contains(final Integer object) {
85+
Entry pred = this.head;
86+
Entry curr = pred.next;
87+
88+
while (curr.object.compareTo(object) < 0) {
89+
pred = curr;
90+
curr = curr.next;
91+
}
92+
return object.equals(curr.object);
93+
}
94+
}
95+
96+
/**
97+
* An implementation of the ListSet interface that uses Java read-write
98+
* locks to protect against concurrent accesses.
99+
*
100+
* TODO Implement the add, remove, and contains methods below to support
101+
* correct, concurrent access to this list. Use a Java
102+
* ReentrantReadWriteLock object to protect against those concurrent
103+
* accesses. You may refer to SyncList.java for help understanding the list
104+
* management logic, and for guidance in understanding where to place
105+
* lock-based synchronization.
106+
*/
107+
public static final class RWCoarseList extends ListSet {
108+
/*
109+
* TODO Declare a read-write lock for this class to be used in
110+
* implementing the concurrent add, remove, and contains methods below.
111+
*/
112+
113+
/**
114+
* Default constructor.
115+
*/
116+
public RWCoarseList() {
117+
super();
118+
}
119+
120+
/**
121+
* {@inheritDoc}
122+
*
123+
* TODO Use a read-write lock to protect against concurrent access.
124+
*/
125+
@Override
126+
boolean add(final Integer object) {
127+
Entry pred = this.head;
128+
Entry curr = pred.next;
129+
130+
while (curr.object.compareTo(object) < 0) {
131+
pred = curr;
132+
curr = curr.next;
133+
}
134+
135+
if (object.equals(curr.object)) {
136+
return false;
137+
} else {
138+
final Entry entry = new Entry(object);
139+
entry.next = curr;
140+
pred.next = entry;
141+
return true;
142+
}
143+
}
144+
145+
/**
146+
* {@inheritDoc}
147+
*
148+
* TODO Use a read-write lock to protect against concurrent access.
149+
*/
150+
@Override
151+
boolean remove(final Integer object) {
152+
Entry pred = this.head;
153+
Entry curr = pred.next;
154+
155+
while (curr.object.compareTo(object) < 0) {
156+
pred = curr;
157+
curr = curr.next;
158+
}
159+
160+
if (object.equals(curr.object)) {
161+
pred.next = curr.next;
162+
return true;
163+
} else {
164+
return false;
165+
}
166+
}
167+
168+
/**
169+
* {@inheritDoc}
170+
*
171+
* TODO Use a read-write lock to protect against concurrent access.
172+
*/
173+
@Override
174+
boolean contains(final Integer object) {
175+
Entry pred = this.head;
176+
Entry curr = pred.next;
177+
178+
while (curr.object.compareTo(object) < 0) {
179+
pred = curr;
180+
curr = curr.next;
181+
}
182+
return object.equals(curr.object);
183+
}
184+
}
185+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package edu.coursera.concurrent;
2+
3+
/**
4+
* A single element in any of the list implementations.
5+
*/
6+
public final class Entry {
7+
/**
8+
* The value stored in this list entry.
9+
*/
10+
public final Integer object;
11+
12+
/**
13+
* The next element in this singly linked list.
14+
*/
15+
public Entry next;
16+
17+
/**
18+
* The general constructor used when creating a new list entry.
19+
*
20+
* @param setObject Value to store in this item
21+
*/
22+
Entry(final Integer setObject) {
23+
this.object = setObject;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package edu.coursera.concurrent;
2+
3+
/**
4+
* The abstract interface implemented by each of the List versions tested in
5+
* this mini-project. Lists that support this interface must be able to add
6+
* objects, remove objects, and test for existence of an object. These methods
7+
* are required to maintain a sorted list of items internally with no
8+
* duplicates.
9+
*/
10+
public abstract class ListSet {
11+
/**
12+
* Starting entry of this concurrent list.
13+
*/
14+
protected final Entry head;
15+
16+
/**
17+
* Default constructor.
18+
*/
19+
public ListSet() {
20+
this.head = new Entry(Integer.MIN_VALUE);
21+
this.head.next = new Entry(Integer.MAX_VALUE);
22+
}
23+
24+
/**
25+
* Getter for the head of the list.
26+
*
27+
* @return The head of this list.
28+
*/
29+
public Entry getHead() {
30+
return head;
31+
}
32+
33+
/**
34+
* Add an integer value to this sorted list, ensuring uniqueness. This
35+
* method must use ListSet.head as the head of the list.
36+
*
37+
* @param o The integer to add.
38+
* @return false if this value already exists in the list, true otherwise
39+
*/
40+
abstract boolean add(Integer o);
41+
42+
/**
43+
* Remove an integer value from this list if it exists. This method must use
44+
* ListSet.head as the head of the list.
45+
*
46+
* @param o The integer to remove.
47+
* @return true if this value is found in the list and successfully removed,
48+
* false otherwise
49+
*/
50+
abstract boolean remove(Integer o);
51+
52+
/**
53+
* Check if this list contains the provided value. This method must use
54+
* ListSet.head as the head of the list.
55+
*
56+
* @param o The integer to check for.
57+
* @return true if this list contains the target value, false otherwise.
58+
*/
59+
abstract boolean contains(Integer o);
60+
}

0 commit comments

Comments
 (0)