Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between these methods?

 
Wilson Hui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TestClass {
public static synchronized int methodA() { ... }
public static int methodB() { ... }
public synchronized int methodC() { ... }
public int methodD() { ... }
}
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic