• 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

Connections in static methods?

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

I need to refactor some code because I have no idea how this is supposed to work in a multi-threaded environment. So here is what's going on:
- The UI is reading XMLs from a URLs. In order to avoid repetition of same code everywhere, methods like connect method (to open URLConnection and read xml string) and getDocument (to create a Document object from URL) were made static and are being used as Util.getConnection() or Util.getDocument() all over the code.

This is working fine until now, but I am not confident if this will still work when there are more than 1 person accessing the UI?

Any thoughts on this?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We'd have to see the code to know for sure. When you call Util.getConnection(), does it just return the opened connection object to the caller (thread safe) or does it store it internally (not thread safe)? How are connections closed when you're finished with them? I assume that Util.getDocument() must have a connection to work with. Where is that coming from?

Threads are a difficult topic. Try to walk through imaginary scenarios where one thread gets part way through its work, but then has to wait while the second thread runs. Can something the first thread needs be modified by the second thread?

This topic is too complicated for beginners, and it is mainly about multi-threading issues, so I'll move it over to the Threads and Synchronization forum.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code in Util class:


I am not returning any Connection here:


So, are these methods thread safe
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, those look okay by themselves. If everything that is created is held as local variables, you are fine because each Thread gets its own copy of local variables. The only question comes out of shared data (data that is stored as static or instance variables, or which are passed in as parameters or returned as results).

From the code you provided, the only piece of the puzzle you would need to worry about is the Document. The Document is mutable, so if multiple threads had a hold on the same Document they could cause problems by simultaneously changing it, so you would need to make sure that the Document doesn't leak to multiple threads. That isn't something you can do in this Util class though.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Steve, your reply is helpful. So how do I make sure that the Document does not leak to multiple threads? I hope this can be done in the object that calls this method.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic