views:

170

answers:

2

Hi.

I'm creating a static class which is going to hold some vectors with info. I have to make it synchronized so that the class will be locked if someone is editing or reading from the vectors.

What is the best way to do this?

Is it enough to have a function which is synchronized inside the class like this:

public synchronized insertIntoVector(int id)
{

}

Thanks in advance :)

+5  A: 

Firstly, you need to define exactly what you mean by "static class". At first, I thought you meant a class where all methods were static (that wasn't meant to be instantiated) - but your code snippet implies this isn't the case.

In any case, synchronized methods inside the class are equivalent to synchronized(this) if they are instance methods, or synchronized(TheContainingClassName.class) if they're static methods.

If you are either creating a non-instantiable class with all static methods, or if you are creating a class that will act as a singleton, then synchronizing every method of the class will ensure that only one thread can be calling methods at once.

Do try to ensure that your methods are atomic though, if possible; calls to different methods can be interleaved by other threads, so something like a getFoo() call followed by a setFoo() (perhaps after incrementing the foo variable) may not have the desired effect if another thread called setFoo() inbetween. The best approach would be to have a method such as incrementFoo(); alternatively (if this is not possible) you can publish the synchronization details so that your callers can manually hold a lock over the class/instance during the entire sequence of calls.

Andrzej Doyle
What i want is a class that should not be instantiated. But when some of the functions is used, the whole class should be locked.
It seems that Andrzej Doyle was right in assuming what you wanted to say. Conclusión: synchronized static methods are enough because all of them sync against YourClass.class. And be careful with what you code inside the methods, too!!
helios
Thanks for the answer :)
An additional Point:If you access the vector only within the synced methods, you can use an ArrayList instead of Vector. Your prior synchronisation will prevent unsynchronized access, so you don't need the additional sync done by Vector
Hardcoded
Thanks for the additional info @Hardcoded, changing to ArrayList now :)
+1  A: 

AFAIK, there's no such thing as "static class" in Java. Do you mean a class that contains only static methods? If so, then

public static synchronized void insertIntoVector(int id) {

}

synchronizes with respect to the class object, which is sufficient, if there are only static methods and all of them are synchronized.

If you mean static inner class (where the word "static" has a different meaning than in static methods), then

public synchronized void insertIntoVector(int id)
{

}

synchronizes with respect to an instance of that static inner class.

Joonas Pulakka
Thanks for the answer :)