Can anyone offer some kind help? Thanks a lot!
HashMap aren't thread safe, meaning that it will get corrupted if it gets updated by different threads.
The thread safe version of HashMap is ConcurrentHashMap.
But i'm not sure that it's actually a problem of thread safety.
At the end of your code you check if tCustomer equals null, which it does.
But after that you try to print tCustomer.getName(), which throws the NullPointerException.
First make sure that the maps are properly populated.
First it is sure that the HashMap is well populated in the beginning. I tested the HashMap in the createInitialTariffs (), and it is right. But after handling the message function: handleMessage(TariffTransaction ttx), the HashMap cannot be accessed.
In createInitialTariffs (), it is right.
1. Interruptible_consumption includes thermal_storage_consumption, Consumption includes Interruptible_consumption.
2. the field of Value should not be zero, but the population of the subscribed customers.
I just tested the code on SampleBroker, with no other modifications. I am curious why the map does not work between threads. Or do I have to change to other data structure, like vector<>, but that would be unconvenient.
In fact, the map for Interruptible_consumption and Consumption go wrong randomly in each test, but the maps for other consumers are right.
I wonder if it is concerned to the environment where I test the sample broker. I tested the sample broker in the simulation environment in my local machine.
It is not necessary to use a ConcurrentHashMap. In the sample broker MarketManagerService and PortfolioManagerService, all the handleMessage() methods (except the one for CustomerBootstrapData) are synchronized, and the activate() methods are synchronized. Those are the only entry points after initialization. Probably the handler for CustomerBootstrapData should also be synchronized; I considered that to be part of initialization, but I am not entirely confident that the lack of synchronization on that method won't cause a problem.