Skip to content

Commit

Permalink
More testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mishadoff committed Oct 29, 2013
1 parent 9db1d4c commit 488bc1d
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 47 deletions.
17 changes: 16 additions & 1 deletion src/concurrency/stm/Ref.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public final class Ref<T> {
RefTuple<T, Long> content;

public Ref(T value) {
content = RefTuple.get(value, 0);
content = RefTuple.get(value, 0L);
}

public T getValue(Context ctx) {
Expand All @@ -17,4 +17,19 @@ public T getValue(Context ctx) {
public void setValue(T value, Transaction tx) {
tx.set(this, value);
}

/*
UNSAFE
ONLY FOR INSTRUMENTATION
*/

@Deprecated
public T get() {
return content.value;
}

@Deprecated
public void set(T value) {
this.content.value = value;
}
}
8 changes: 3 additions & 5 deletions src/concurrency/test/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
import concurrency.stm.Ref;

public class Account {
private long money;
private Ref<Long> moneyRef;

Account(long initialMoney) {
money = initialMoney;
moneyRef = new Ref<Long>(money);
moneyRef = new Ref<Long>(initialMoney);
}

public void add(long amount) {
money += amount;
moneyRef.set(moneyRef.get() + amount);
}

long getMoney() {
return money;
return moneyRef.get();
}

public Ref<Long> getRef() {
Expand Down
37 changes: 8 additions & 29 deletions src/concurrency/test/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,21 @@
import java.util.concurrent.TimeUnit;

public class Bank {
private Account[] accounts;
Account[] accounts;
private Random random = new Random();

long total = 0;
double charge = 0;

public Bank() {
randomFill();
}

synchronized void transfer(Account a1, Account a2, int amount) {
a1.add(-amount);
a2.add(amount);
}

void transferSTM(final Account a1, final Account a2, final int value) {
STM.transaction(new TransactionBlock() {
@Override
public void run() {
Transaction tx = this.getTx();
long old1 = a1.getRef().getValue(tx);
a1.getRef().setValue(old1 - value, tx);
long old2 = a2.getRef().getValue(tx);
a2.getRef().setValue(old2 + value, tx);
}
});
}

private void randomFill() {
int NUM = 100;
accounts = new Account[NUM];
for (int i = 0; i < NUM; i++) {
accounts[i] = new Account(10000);
accounts[i] = new Account(100000);
}
}

Expand All @@ -46,7 +31,7 @@ public Account getRandomAccount() {
}

public int getRandomValue() {
return random.nextInt(10);
return random.nextInt(100);
}

public long sum() {
Expand All @@ -55,17 +40,11 @@ public long sum() {
return sum;
}

public long sumSTM() {
long sum = 0;
for (Account a : accounts) sum += a.getRef().getValue(GlobalContext.get());
return sum;
}

public void simulate(int threads, int num) throws Exception{
public void simulate(int threads, int num, TransferStrategy ts) throws Exception{
ExecutorService service =
Executors.newFixedThreadPool(threads);
for (int i = 0; i < threads; i++) {
service.submit(new BankThread(this, num));
service.submit(new BankThread(this, num, ts));
}
service.shutdown();
service.awaitTermination(1, TimeUnit.MINUTES);
Expand Down
10 changes: 6 additions & 4 deletions src/concurrency/test/BankThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
public class BankThread implements Runnable {
private Bank bank;
private int num;
private TransferStrategy ts;

public BankThread(Bank bank, int num) {
public BankThread(Bank bank, int num, TransferStrategy ts) {
this.bank = bank;
this.num = num;
this.ts = ts;
}

@Override
public void run() {
for (int i = 0; i < num; i++) {
bank.transferSTM(bank.getRandomAccount(),
bank.getRandomAccount(),
bank.getRandomValue());
ts.transfer(bank.getRandomAccount(),
bank.getRandomAccount(),
bank.getRandomValue());
}
}
}
12 changes: 12 additions & 0 deletions src/concurrency/test/NonSyncStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package concurrency.test;

/**
* @author mishadoff
*/
public class NonSyncStrategy implements TransferStrategy {
@Override
public void transfer(Account a, Account b, int amount) {
a.add(-amount);
b.add(amount);
}
}
4 changes: 3 additions & 1 deletion src/concurrency/test/RefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public static void main(String[] args) {

Bank bank = new Bank();

bank.transferSTM(a, b, 10);
new STMStrategy().transfer(a, b, 10);
new STMStrategy().transfer(a, b, 10);
new STMStrategy().transfer(a, b, 10);

System.out.println("A: " + a.getRef().getValue(GlobalContext.get()));
System.out.println("B: " + b.getRef().getValue(GlobalContext.get()));
Expand Down
23 changes: 16 additions & 7 deletions src/concurrency/test/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

public class Runner {
public static void main(String[] args) throws Exception{
Bank bank = new Bank();
System.out.println("Bank sum before: " + bank.sumSTM());
long before = System.currentTimeMillis();
bank.simulate(10, 100000);
long after = System.currentTimeMillis();
System.out.println("Bank sum after: " + bank.sumSTM());
System.out.println("Elapsed time: " + (after - before));
TransferStrategy[] tss = new TransferStrategy[] {
new NonSyncStrategy(),
new SyncStrategy(),
new STMStrategy()
};

for (TransferStrategy ts : tss) {
Bank bank = new Bank();
System.out.println(ts.getClass().getSimpleName());
System.out.println("Bank sum before: " + bank.sum());
long before = System.currentTimeMillis();
bank.simulate(100, 10000, ts);
long after = System.currentTimeMillis();
System.out.println("Bank sum after: " + bank.sum());
System.out.println("Elapsed time: " + (after - before));
}
}
}
24 changes: 24 additions & 0 deletions src/concurrency/test/STMStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package concurrency.test;

import concurrency.stm.STM;
import concurrency.stm.Transaction;
import concurrency.stm.TransactionBlock;

/**
* @author mishadoff
*/
public class STMStrategy implements TransferStrategy {
@Override
public void transfer(final Account a, final Account b, final int amount) {
STM.transaction(new TransactionBlock() {
@Override
public void run() {
Transaction tx = this.getTx();
long old1 = a.getRef().getValue(tx);
a.getRef().setValue(old1 - amount, tx);
long old2 = b.getRef().getValue(tx);
b.getRef().setValue(old2 + amount, tx);
}
});
}
}
12 changes: 12 additions & 0 deletions src/concurrency/test/SyncStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package concurrency.test;

/**
* @author mishadoff
*/
public class SyncStrategy implements TransferStrategy{
@Override
public synchronized void transfer(Account a, Account b, int amount) {
a.add(-amount);
b.add(amount);
}
}
8 changes: 8 additions & 0 deletions src/concurrency/test/TransferStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package concurrency.test;

/**
* @author mishadoff
*/
public interface TransferStrategy {
void transfer(final Account a, final Account b, final int amount);
}

0 comments on commit 488bc1d

Please sign in to comment.