Reputation: 823
I've created a StringUtil class which is used for some string validation across the application. The code for the StringUtil is as below,
public class StringUtil {
public static synchronized boolean isValidString(String string) {
return string!= null && string.trim().length() > 0;
}
}
In this class the method checks whether the string is a valid string or not. This method is thread safe. In an enterprise application, there might be multiple threads accessing this method. If a thread is accessing this method, then all the other threads have to wait for its turn. This method in turn will be used very frequently to check the string for null values. So which is the best option
Upvotes: 2
Views: 1900
Reputation: 10650
Utility classes usually contain only static methods therefore it always a good idea to state explicitly that these classes were not designed to be instantiated. Therefore make their constructor private:
public class StringUtils {
private StringUtils() {
throw new AssertionError("shouldn't be instantiated");
}
}
(see Joshua Bloch's Bible : Item 4: Enforce noninstantiability with a private constructor )
Upvotes: 1
Reputation: 19185
There is no need of synchronized
keyword since String is immutable object
Immutable objects are often useful because they are inherently thread-safe
public static boolean isValidString(String string) {
return !(string== null || string.isEmpty()); //Since 1.6
}
Upvotes: 1
Reputation: 4847
You should probably use the StringUtils
class in Apache Commons.
Upvotes: 3
Reputation: 115398
This method should not be synchronized because it does not use any class level variables. So multiple threads can concurrently access it without any problem.
Moreover forget about synchronization when you are writing code for enterprise application that is running into container. It is container's responsibility to care about thread safety. synchronized
blocks just bother the container to do its job. If you need synchronization in enterprise application re-think your design and/or find other patters (there are a lot) to solve your problem.
Upvotes: 1
Reputation: 30007
Usually helper methods like this, are public static
, and not synchronized
, because the class doesn't hold state. Since it doesn't hold state neither, you don't need a pool.
I think a good example of this the apache commons StringUtils class.
I have the feeling that you're trying to use a neutron cannon to open a walnut, simplicity is king :)
Upvotes: 7
Reputation: 22925
You could try the util classes from Apache Commons.
But you have thread-safety here anyway, since you're not manipulating anything in the class that other calls might read (i.e. you have no state).
Upvotes: 3
Reputation: 46239
Since you don't have any state here (you only use the method argument string
), the method is inherently thread safe. Therefore there is no need to use the synchronized
keyword.
If the method is used throughout your project, just declaring it static
as you have already done is the best option.
Upvotes: 16