• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

when the HashMap instance need synchronized?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an class will be run under a mutilthreaded environ,the class like this
public class ReportEngine
{
private HashMap publicReport;
private Report getReportData(String reportKey) {
return publicReport.get(reportKey);
}
private void deleteReport(String reportKey) {
publicReport.remove(reportKey);
}
private void addReport(String reportKey,Report report) {
publicReport.add(reportKey,report);
}
}
So, what I am puzzled are:
1)which methods need synchronized ?
2)what will happen if don't synchronize any methods ?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)which methods need synchronized ?
Both deleteReport and addReport
2)what will happen if don't synchronize any
methods ?
your program may lost data or crush
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also replace the HashMap with a Hashtable unless you specifically want a HashMap or you are permitting null values.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugh. Let Hashtable die the death it deserves (along with Vector and Enumeration). There's no need for those things anymore; they just create additional opportunities for confusion, particularly for newbies. If you really want a synchronized Map, use Collections.synchronizedMap(new HashMap()). And pay attention to the warnings about iterating - these would have applied to Hashtable too.
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
******** Use a synchronized HashMap - Collections.synchronizedMap(new HashMap());
Also - the get method would also have to be synchronized if for some reason you don't want a synchronized map (why not?). This is because the HaspMap could be resizing itself during the get operation and really throw it off.
 
I don't like that guy. The tiny ad agrees with me.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic