Skip to content

Commit 54c74bb

Browse files
committed
add project1
1 parent e437f70 commit 54c74bb

File tree

17 files changed

+1463
-0
lines changed

17 files changed

+1463
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Directories #
2+
/build/
3+
/bin/
4+
target/
5+
# OS Files #
6+
.DS_Store
7+
*.class
8+
# Package Files #
9+
*.jar
10+
*.war
11+
*.ear
12+
*.db
13+
######################
14+
# Windows
15+
######################
16+
# Windows image file caches
17+
Thumbs.db
18+
# Folder config file
19+
Desktop.ini
20+
######################
21+
# OSX
22+
######################
23+
.DS_Store
24+
.svn
25+
# Thumbnails
26+
._*
27+
# Files that might appear on external disk
28+
.Spotlight-V100
29+
.Trashes
30+
######################
31+
# Eclipse
32+
######################
33+
*.pydevproject
34+
.project
35+
.metadata
36+
bin/**
37+
tmp/**
38+
tmp/**/*
39+
*.tmp
40+
*.bak
41+
*.swp
42+
*~.nib
43+
local.properties
44+
.classpath
45+
.settings/
46+
.loadpath
47+
/src/main/resources/rebel.xml
48+
# External tool builders
49+
.externalToolBuilders/
50+
# Locally stored "Eclipse launch configurations"
51+
*.launch
52+
# CDT-specific
53+
.cproject
54+
# PDT-specific
55+
.buildpath
56+
57+
# For Android Project
58+
# built application files
59+
*.apk
60+
*.ap_
61+
62+
# files for the dex VM
63+
*.dex
64+
65+
# Java class files
66+
*.class
67+
68+
# generated files
69+
bin/
70+
gen/
71+
72+
#libraries
73+
libs/
74+
75+
#logs
76+
*.log
77+
78+
# Local configuration file (sdk path, etc)
79+
local.properties
Lines changed: 91 additions & 0 deletions
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>
Lines changed: 185 additions & 0 deletions
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+
}
Lines changed: 25 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)