简体   繁体   English

字符串实用程序-Java

[英]String Utils - Java

I've created a StringUtil class which is used for some string validation across the application. 我创建了一个StringUtil类,该类用于整个应用程序中的某些字符串验证。 The code for the StringUtil is as below, StringUtil的代码如下所示,

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. 由于此处没有任何状态(您仅使用方法参数string ),因此该方法本质上是线程安全的。 Therefore there is no need to use the synchronized keyword. 因此,无需使用synchronized关键字。

If the method is used throughout your project, just declaring it static as you have already done is the best option. 如果在整个项目中都使用该方法,那么最好将其声明为static是最好的选择。

Usually helper methods like this, are public static , and not synchronized , because the class doesn't hold state. 通常这样的辅助方法是public static ,而不是synchronized ,因为该类不持有状态。 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. 我认为这是apache commons StringUtils类的一个很好的例子。

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. 您可以尝试来自Apache Commons的util类。

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. synchronized块只会打扰容器来完成其工作。 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 字符串是不可变的对象,因此不需要synchronized关键字

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 ) (请参阅约书亚·布洛赫Joshua Bloch)的圣经》 :第4项:使用私有构造函数强制执行非不稳定性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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