wood burning stoves 2.0*
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
Author

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

Thanks
Bala
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
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.


Ping & DNS - my free Android networking tools app
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

Start with concurrent hash map ...

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ConcurrentHashMap.html


"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
Sunny Jain
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: http://aspose.com/file-tools
 
subject: Share data between threads
 
Similar Threads
iterate through a hashMap
How to store an XML file as a string in database and retrieve in Java code
HashMap or HashTable
Collections/HashMap and threads
Multiple threads and HashMap