Meaningless Drivel is fun!
The moose likes Threads and Synchronization and the fly likes Share data between threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Share data between threads" Watch "Share data between threads" New topic

Share data between threads

Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
Hello Everyone,
I have a 50 java threads and each thread will watch 10 stocks in real time. Here all threads should update the data to single HashMap. The data should be updated every second. So that i can take data from the hashmap and use it for further processing. Could you please let me know which is the best way to achieve this?

Thanks much in advance

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
You can pass the HashMap to each thread in the constructor. Be sure to synchronize access to the HashMap where necessary. Using a ConcurrentHashMap may be a better option.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 443

Start with concurrent hash map ...

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Balasubramaniam Muthusamy
Ranch Hand

Joined: Nov 30, 2010
Posts: 51
Thank you much for your prompt answer. I will look into that
Deepak Chopra
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

I know this may be out of subject here, but updating stock data every second..In my opinion its better if you update once in every few milliseconds..
TimeUnit class in JAVA 5+ can be also helpful to you..If you want to go for nanoseconds, microseconds, milliseconds, ...or hours .

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
I agree. Here's the link:
subject: Share data between threads
It's not a secret anymore!