This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes What is the difference between these methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What is the difference between these methods?" Watch "What is the difference between these methods?" New topic
Author

What is the difference between these methods?

Wilson Hui
Greenhorn

Joined: Jul 18, 2002
Posts: 6
public class TestClass {
public static synchronized int methodA() { ... }
public static int methodB() { ... }
public synchronized int methodC() { ... }
public int methodD() { ... }
}
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is the difference between these methods?