• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loadin class VS Storing it in HashMap and taking it back for further use

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have one question, I am in confusion , what is good if we compare loading class every time and using it for operation vs first time loading it and then store it in HashMap for further processing.

Thanks in advance
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain in more detail (maybe with an example) what you mean exactly?

As far as I know, when you load a class in Java, it will never be unloaded (unless the class loader that loaded it is garbage collected itself). You don't need to manage loading classes yourself, the class loader does that for you. But I suspect that by "loading a class" you might mean something else than the Java class loading process?
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think he means:





vs:


The HashMap seems to be an added dimension to the above approach

Is that your question amod ?
 
amod gole
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Application is like this

one EJB which is calling different TaskHandler(interface implemented by different TaskHandler classes) for different Task. In Our application what we are doing we defined one HaspMap and in that we are storing these different TaskHandler so when new requests come to our EJB instead of loading TaskHandler we are checking availability of that taskHandler in HashMap if it is present then we are using that if not then we are loading it and storing it in same hashmap.

SO my question was what is best loading each time taskHandler means defining new object of task Handler or storing it in hashmap?. My EJB type is stateless and lets say 15 different taskHandler.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha, so as I suspected, your question is not about class loading at all. So, to phrase it differently, your question is the following.

Your application has a TaskHandler interface and several classes that implement that interface, and now there's a HashMap that contains instances of those classes, and for each request the relevant TaskHandler is looked up in the HashMap and called. You're asking why that HashMap is being used, instead of just creating a new instance of the relevant TaskHandler for each request.

Probably the HashMap is meant as a cache, to avoid creating a lot of objects (instances of the classes that implement interface TaskHandler). If those classes are also stateless, it's not necessary to create multiple instances of those classes. Not creating a new object for each request would also make the application more scalable.
 
amod gole
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks A Bunch........
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic