This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Share data between threads  RSS feed

 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with concurrent hash map ...

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ConcurrentHashMap.html
 
Balasubramaniam Muthusamy
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you much for your prompt answer. I will look into that
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!