Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Is Java Synchronized Method is applicable for only existing object?

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

I have a public class called Template and have a method called translate()

It is a multi-thread envrionment. I want only one thread to access the method at one point of time so that no time out of memory exception will occur

So i made the method as sychronized...i.e add the method signature with synchronized..

However many execution is running simultaneously.....

Wondering why this is happening???

Each thread will create new Template() object and call the translate() method

Is Java Synchronized Method is applicable for only existing object?

Please kindly help me to understand the code.
 
Saloon Keeper
Posts: 14327
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joseph Michael
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Marshal
Posts: 27379
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic