synchronization between threads

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

synchronization between threads

Sheldon
Hello, everyone
      I am a new developer on PowerTAC broker, and I am new in Java. I have developed my own broker based on the sample broker. In debugging, I found that HashMap data are not correctly synchronized between the message handling thread and working thread (where activate() works). As HashMap is not safe in multi-thread, I tried HashTable, but it still did not synchronize correctly. Hash related structure is proper in PortfolioManagementService, but it does not work well between threads. Do you have any good suggestions? Are there any better ways to synchronize the data between message thread and working thread?
      Thanks for your attention!
Reply | Threaded
Open this post in threaded view
|

Re: synchronization between threads

grampajohn
Administrator
Hello, Sheldon -
Sheldon wrote
In debugging, I found that HashMap data are not correctly synchronized between the message handling thread and working thread (where activate() works). As HashMap is not safe in multi-thread, I tried HashTable, but it still did not synchronize correctly. Hash related structure is proper in PortfolioManagementService, but it does not work well between threads. Do you have any good suggestions? Are there any better ways to synchronize the data between message thread and working thread?
Very good question. As a practical matter, the sample broker should not run into synchronization problems because activation happens after the last message in a timeslot, and there's a 2-second window (minus network latency, I suppose) where there should be no new messages arriving. But this is not something we should rely on, and I'm sure many interesting broker designs will want to be doing something while messages arrive.

Although in this case you could switch to Hashtable, which is synchronized, it's probably better to just synchronize the methods that might access data structures that are (potentially) shared across threads. That works as long as you don't do long-running operations in your synchronized methods. For such cases, you are better off using a work queue with synchronized access methods.

The broker core uses synchronized methods to deal with this problem. It's possible that I've missed a few; if anyone knows of a problem, let me know. In the meantime, I have opened Issue #811 to track this problem and its solution.

Cheers -

John