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
class CustomerDb2 { | |
private final List<Customers> customers = new ArrayList<>(); | |
private void addCustomer(Customer c) { | |
synchronized(customers) { | |
customers.add(c); | |
} | |
} |
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
SharedThreadContext threadContext = {...} | |
final YourProtectedData[] ypd = new YourProtectedData[1]; | |
threadContext.own(stc -> { | |
YourProtectedData innerYpd = stc.getProtectedData(); | |
ypd[0] = innerYpd; | |
}, logContext); | |
ypd[0].incrementSomeData(); // FAILS with exception: An attempt was made (...) |
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
SharedThreadContext stc = {...}; | |
YourProtectedData ypd = stc.getProtectedData(); // FAILS with exception: An attempt was made to call a method in shared context without owning the required lock. |
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
Integer someData = 10; | |
SharedThreadContext threadContext = new SharedThreadContext(someData); | |
threadContext.own(stc -> { | |
YourProtectedData ypd = stc.getProtectedData(); | |
int newValue = ypd.incrementSomeData(); | |
System.out.println("New value is "+newValue); | |
}, logContext); |
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
class CustomerDb3 { | |
private final List<Customers> customers = new ArrayList<>(); | |
private void countOrders(Customer c) { | |
List<Order> orders; | |
synchronized(c) { | |
orders = c.getOrder(); | |
} |
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
class CustomerDb { | |
List<Customers> customers = new ArrayList<>(); | |
synchronized List<Customers> getCustomers() { | |
return customers; | |
} | |
} | |
class CustomerUpdater { |
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
class Customer { | |
String name; | |
List<Order> orders = new ArrayList<>(); | |
// getters / setters for above. | |
} |