Yes, synchronized uses the current object as the monitor when applied to an instance method. To lock an instance method across multiple objects, you need to synchronize on a shared lock:
This is very suspect though. Why do you fear running out of memory when running the translate() method from multiple threads? The first thing to do would be to analyze the memory consumption and find and fix the greatest resource hogs.
Stephan, a translation takes 1GB of memory...So multiple threads goes to out of memory exception...So we don't want to allow them...Earlier i though if we make a method as synchronized, it will be applicable always...No matter new or old objects...Now i understand how the thread works...
If you want to write some code which processes the translations one at a time, then don't mess about trying to write threads to do that. Instead, use an ExecutorService to take care of all the ugly details like the one you're asking about here. Like this:
Then you can just call the submit method to pass it your translations and have them processed one at a time. Like this:
I'm sure glad that he's gone. Now I can read this tiny ad in peace!