• 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

Protecting a method's argument references in threads

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am facing a ttypical issue with usage of threads in my application. I have a singleton object with a method like public List getXXX(List list1). This method is being hit by several threads. The problem is have is that before the method does something useful , some other thread comes along and changes the reference of list1 to a different list. I thought of making this method synchronized, but doing so will make my application of no use. Basically this application splits a big method call in to smaller calls but spawning n number of threads and splitting the list1 parameter accordingly. If I sychronize the method, the final outcome will be much slower than calling the method without any threads.


Any help in this regard will be greatly appreciated.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have definitely hit a dilemma in all this. You split work out to multiple threads so one can run while another waits on something, say file io or a call to a remote system. But in the middle of this work all the threads have to touch some shared resource.

You don't have much choice but to serialize the access to the shared resource through synchronization or tricky wait & notify. All I can suggest is to synchronize the smallest possible part of the task and allow the rest of the task to run in parallel in many threads.

Or can you eliminate the shared resource part and send each thread a subset of List1? A copy of List1?
[ November 02, 2005: Message edited by: Stan James ]
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The threads need to do their work outside of synchronization. it does not matter if they need to synchronize to get a work package.

Also can you rephrase your question? it sounds like you are passing in a shared resource as opposed to getting one out of that method!?
reply
    Bookmark Topic Watch Topic
  • New Topic