last day (14 days later) » 

10:42 AM
1
A: How to modify class design to make it thread safe

weston a modification to the map might occur using update bidder and getBidders might be called before it is completed leaving inconsistent state You're using a ConcurrentMap, it is built to cope with those situations already. The use of statics, injecting the map in a constructor, returning the m...

 
Thanks for the reply - i though as much when i read the comment but avoid returning the internals of this class - however if another object needs to know the current bidders how would one go about that without returning the internals of the class
also regarding the singleton - is that not considered an anti pattern?
 
What does the other class need to know exactly?
 
What bidders there are available at that time
 
Singleton is, that's why I recommend caution, but when you declare a static field, that's a single instance for the whole program, so you had that anyway.
Just the names of the current bidders? Show me the code that uses that.
 
10:42 AM
Hi
 
so for example, the class i showed you a second ago can be used un the following way
The actual classname is players by the way - not bidders just changed it for the sake of putting my code on the internet - however it iused like this
private synchronized void computeOverallResults() {

if(liveBets.size() > 0){
Object[][] playerResults = new Object[Players.getPlayers().size()][];

int index = 0;
for (Map.Entry<String, Tuple<Double, Double>> entry : Players.getPlayers().entrySet()) {
String user = entry.getKey();
playerResults[index] = new Object[]{user, Players.getPlayers().get(user).getFirst(), Players.getPlayers().get(user).getSecond()};
index ++;
}
System.out.println("");
System.out.println("The leaderboard looks as follows: ");
so here i am directly accessing my class to get the available players (previously known to you as bidders)
 
But really you only want the names. You're returning too much.
 
i will need the keys and the values at different places - I think potentially i could provide a get map mechanism which has a lock, so only one thread can get it at a time?
 
Imagine how much cleaner it would look as: for(String user : players.getPlayerNames())
 
that is very true - however i will need the value of the player <Key=Playername, value=<BetValues>
 
10:49 AM
Well that could be a separate call, I'd return a List<PlayerBets>. Where PlayerBet is a player and their bet.
 
ah ok, so rathe than return the whole map - we can break the tasks down in to individual methods
that makes it more thread safe?
 
No, it's threadsafe to return the concurrentmap, because that's already threadsafe. This is about encapsulation. You only want one way of updating a bet, if you return the map, the code that gets that map could call put on it or it call clear, or any of the maps methods. With the list we return we don't care what they do it won't affect the integrity of the data within Bidders
 
Ok that makes perfect sense - sorry to be a right pain in the ass - I have one final question if thats ok
 
But then actually, while processing the list, you will need some additional concurrency. I'm working on an example...
Sure, ask
 
This might be a silly question :$ - If we have a private final variable, (not static) and we need the map to be referenced from multiple places (or at least we need to call methods that get data from the map in multiple classes) how do we ensure we are always working on the same Players reference?
Do we create the players object from the outset and pass the reference around as and when it is needed?
 
11:03 AM
Yes, inject players where needed into constructors or methods. Because that's a pain, this is where the singleton pattern is tempting, but also where it causes problems. With singtleton, if you start a new game, it'll have new players potentially and you will need to clear the old players and it all starts to get messy.
 
I see - thank you for your help - essentially the reason i chose the approach in the original outset was because passing the reference around meant updates might not be seen from other classes holding the reference, where as if they accessed the data structure directly they would all be using a common data set
hopefully you can understand my logic :D
 
11:16 AM
Yes, I see, I've updated the code in my answer. Now each consumer of Bidders will use getBids and see a current view on the data, but not be able to modify it.
 
will take a look - thank you so much for your help
 
That's OK, good luck with it!
 
just as a final question - how long did it take you to be comfortable with this sort of programming - its quite hard :)
 

last day (14 days later) »