This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 41574
    
  54
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: 416
    
    2

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
 
jQuery in Action, 2nd edition
 
subject: Share data between threads