Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Protecting a method's argument references in threads  RSS feed

 
Srinivas Modekurti
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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!?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!