Skip to content

Commit 62841d8

Browse files
committed
add project2 concurrent programming
1 parent 36dd20d commit 62841d8

File tree

9 files changed

+673
-0
lines changed

9 files changed

+673
-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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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_2</artifactId>
7+
<packaging>jar</packaging>
8+
<version>0.0</version>
9+
<name>miniproject_2</name>
10+
11+
<properties>
12+
<pcdp.version>0.0.4-SNAPSHOT</pcdp.version>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
</properties>
15+
16+
<repositories>
17+
<repository>
18+
<id>pcdp-repo</id>
19+
<url>https://raw.github.com/habanero-maven/hjlib-maven-repo/mvn-repo-pcdp-${pcdp.version}/</url>
20+
<snapshots>
21+
<enabled>true</enabled>
22+
<updatePolicy>always</updatePolicy>
23+
</snapshots>
24+
</repository>
25+
</repositories>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-resources-plugin</artifactId>
31+
<version>2.4.3</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>edu.rice.pcdp</groupId>
35+
<artifactId>pcdp-core</artifactId>
36+
<version>${pcdp.version}</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>3.8.2</version>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<pluginManagement>
49+
<plugins>
50+
<plugin>
51+
<!-- specify the java version to use during compilation -->
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-compiler-plugin</artifactId>
54+
<version>3.1</version>
55+
<configuration>
56+
<source>1.8</source>
57+
<target>1.8</target>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<!-- populates the properties for dependency jar paths -->
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-dependency-plugin</artifactId>
64+
<version>2.9</version>
65+
<executions>
66+
<execution>
67+
<goals>
68+
<goal>properties</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
<plugin>
74+
<!-- executes test with -Xmx option -->
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-surefire-plugin</artifactId>
77+
<version>2.17</version>
78+
<configuration>
79+
<forkMode>pertest</forkMode>
80+
<argLine>-Xmx4g</argLine>
81+
<useSystemClassLoader>true</useSystemClassLoader>
82+
<testFailureIgnore>true</testFailureIgnore>
83+
</configuration>
84+
</plugin>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-checkstyle-plugin</artifactId>
88+
<version>2.17</version>
89+
<executions>
90+
<execution>
91+
<id>checkstyle</id>
92+
<phase>validate</phase>
93+
<configuration>
94+
<configLocation>${basedir}/src/main/resources/checkstyle.xml</configLocation>
95+
<encoding>UTF-8</encoding>
96+
<consoleOutput>true</consoleOutput>
97+
<failsOnError>true</failsOnError>
98+
<failOnViolation>true</failOnViolation>
99+
</configuration>
100+
<goals>
101+
<goal>check</goal>
102+
</goals>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
</plugins>
107+
</pluginManagement>
108+
</build>
109+
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package edu.coursera.concurrent;
2+
3+
/**
4+
* Represents a single account in a bank whose only contents is the balance of
5+
* that account.
6+
*/
7+
public final class Account {
8+
/**
9+
* Unique ID number for this account.
10+
*/
11+
private int id;
12+
13+
/**
14+
* Current balance of this account.
15+
*/
16+
private int balance;
17+
18+
/**
19+
* Constructor.
20+
*
21+
* @param setId The ID for this account
22+
* @param setStartingBalance The initial balance for this account.
23+
*/
24+
public Account(final int setId, final int setStartingBalance) {
25+
this.id = setId;
26+
this.balance = setStartingBalance;
27+
}
28+
29+
/**
30+
* Get the remaining balance in this account.
31+
*
32+
* @return The current balance.
33+
*/
34+
public int balance() {
35+
return balance;
36+
}
37+
38+
/**
39+
* Remove the specified amount from the current balance of the account.
40+
*
41+
* @param amount The amount to subtract from the current balance.
42+
* @return true if it was possible to subtract that amount, false otherwise.
43+
*/
44+
public boolean withdraw(final int amount) {
45+
if (amount > 0 && amount < balance) {
46+
balance -= amount;
47+
return true;
48+
}
49+
return false;
50+
}
51+
52+
/**
53+
* Add the specified amount to the current balance.
54+
*
55+
* @param amount The amount to add.
56+
* @return true if it was possible to add that amount (i.e. amount > 0),
57+
* false otherwise.
58+
*/
59+
public boolean deposit(final int amount) {
60+
if (amount > 0) {
61+
balance += amount;
62+
return true;
63+
}
64+
return false;
65+
}
66+
67+
/**
68+
* A dummy busy work function for simulating more work to increase isolated
69+
* contention. While this work is artificial in this setting, it serves to
70+
* more accurately simulate real-world conditions where a bank transfer is
71+
* not a shared-memory operation (i.e., it must contend with network
72+
* latencies, disk latencies, etc.).
73+
*
74+
* @param srcID The source account ID
75+
* @param destID The destination account ID
76+
*/
77+
private static void busyWork(final int srcID, final int destID) {
78+
for (int i = 0; i < srcID * 100; i++) {
79+
;
80+
}
81+
for (int i = 0; i < destID * 100; i++) {
82+
;
83+
}
84+
}
85+
86+
/**
87+
* Transfer the specified amount from this account to the specified target
88+
* account.
89+
*
90+
* @param amount The amount to transfer.
91+
* @param target The destination of this transfer.
92+
* @return true if the transfer is successful, false otherwise.
93+
*/
94+
public boolean performTransfer(final int amount, final Account target) {
95+
final boolean withdrawSuccess = withdraw(amount);
96+
busyWork(this.id, target.id);
97+
if (withdrawSuccess) {
98+
target.deposit(amount);
99+
}
100+
return withdrawSuccess;
101+
}
102+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package edu.coursera.concurrent;
2+
3+
import static edu.rice.pcdp.PCDP.isolated;
4+
5+
/**
6+
* A thread-safe transaction implementation using global isolation.
7+
*/
8+
public final class BankTransactionsUsingGlobalIsolation
9+
extends ThreadSafeBankTransaction {
10+
/**
11+
* {@inheritDoc}
12+
*/
13+
@Override
14+
public void issueTransfer(final int amount, final Account src,
15+
final Account dst) {
16+
isolated(() -> {
17+
src.performTransfer(amount, dst);
18+
});
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package edu.coursera.concurrent;
2+
3+
import static edu.rice.pcdp.PCDP.isolated;
4+
5+
/**
6+
* A thread-safe transaction implementation using object-based isolation.
7+
*/
8+
public final class BankTransactionsUsingObjectIsolation
9+
extends ThreadSafeBankTransaction {
10+
/**
11+
* {@inheritDoc}
12+
*/
13+
@Override
14+
public void issueTransfer(final int amount, final Account src,
15+
final Account dst) {
16+
/*
17+
* TODO implement issueTransfer using object-based isolation instead of
18+
* global isolation, based on the reference code provided in
19+
* BankTransactionsUsingGlobalIsolation. Keep in mind that isolation
20+
* must be applied to both src and dst.
21+
*/
22+
throw new UnsupportedOperationException();
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package edu.coursera.concurrent;
2+
3+
/**
4+
* An abstract interface for all thread-safe bank transaction implementations to
5+
* extend.
6+
*/
7+
public abstract class ThreadSafeBankTransaction {
8+
/**
9+
* Transfer the specified amount from src to dst.
10+
*
11+
* @param amount Amount to transfer
12+
* @param src Source account
13+
* @param dst Destination account
14+
*/
15+
public abstract void issueTransfer(final int amount, final Account src,
16+
final Account dst);
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Source code from the Java Concurrent Programming Coursera course.
3+
*/
4+
package edu.coursera.concurrent;

0 commit comments

Comments
 (0)