• 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

What is the difference between these methods?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TestClass {
public static synchronized int methodA() { ... }
public static int methodB() { ... }
public synchronized int methodC() { ... }
public int methodD() { ... }
}
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public static synchronized int methodA() { ... }


Public access
Class (not instance) method
Returns int
Synchronized on the class: only one thread at a time can use this method or any other method or block that is also synchronized on the same class.


public static int methodB() { ... }


Public access
Class method
Returns int
Not synchronized - several threads may execute this method simultaneously


public synchronized int methodC() { ... }


Public, returns int
Instance method
Synchronized on the object instance. Only one thread at a time may use this method or any other method or block that is also synchronized on the same object instance


public int methodD() { ... }


Public, returns int, instance method, not synchronized.
 
reply
    Bookmark Topic Watch Topic
  • New Topic