forked from epam-mooc/stm-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
108 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |