简体   繁体   中英

String Utils - Java

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

  1. Making this a singleton and thread safety
  2. Making this as instance method
  3. Is there are any other way to organize a pool with objects of this type and each thread would pick up one and release the object to the pool once done.So thread safety is not a concern and object creation is also not done.
  4. Are are there any open source libraries for the same.

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.

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 :)

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 (ie you have no state).

您可能应该在Apache Commons中使用StringUtils类。

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.

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
}

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM