代码如下:
import com.google.common.base.Charsets;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.springframework.stereotype.Component;
@Component
public class KeyRepository {
private static final HashFunction SHA512 = Hashing.sha512();
private final byte[] b128 = new byte[16];
public byte[] getOrCreateBy(String key) {
SHA512.hashString(key, Charsets.UTF_8).writeBytesTo(b128, 0, b128.length);
return b128;
}
}
上述代码中的变量b128是被线程所共享的,如果线程一执行到SHA512.hashString(key, Charsets.UTF_8).writeBytesTo(b128, 0, b128.length)的时候,线程二也进入了方法,同样也执行了这一行,此时b128中的值已经被线程二改掉了,这时线程一再执行return b128返回的是线程二的值,而不是线程一的值。也就是说,没有控制好并发,线程一在使用的b128的值被线程二改掉了,导致返回错误的b128结果。正确写法应该如下:
import com.google.common.base.Charsets;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.springframework.stereotype.Component;
@Component
public class KeyRepository {
private static final HashFunction SHA512 = Hashing.sha512();
// 局部变量,用完就释放;或者在方法上加锁
public byte[] getOrCreateBy(String key) {
byte[] b128 = new byte[16];
SHA512.hashString(key, Charsets.UTF_8).writeBytesTo(b128, 0, b128.length);
return b128;
}
}
被@Component修饰的对象默认是单例,如果申明成多例,需要如下申明。项目开发中常用到的注解,控制层的@Controller默认多例,业务层的@Service默认单例,持久层(Dao层)的@Repository默认单例,@Component用于对比较中立的类进行注解,默认单例。
@Component
@Scope("prototype")
单例的话,在方法外定义的b128被多个线程共享(共享才需要考虑多线程的影响),如果两个线程同时进入方法,又同时出去一个方法,那b128只能有一个值,肯定一个是对的值,一个是错的值。将b128放入方法内部,(方法内的局部变量)是放在了栈空间,线程私有,没有共享一说,也就不会出现在高并发的情况下,b128值被乱掉的情况。
Java 虚拟机的内存模型分为两部分:一部分是线程共享的,包括 Java 堆和方法区;另一部分是线程私有的,包括虚拟机栈和本地方法栈,以及程序计数器这一小部分内存。对于局部变量,如果是基本类型,会把值直接存储在栈;如果是引用类型,比如String s = new String("sh");会把其对象存储在堆,而把这个对象的引用(指针)存储在栈。
类的成员变量在不同对象中各不相同,都有自己的存储空间(成员变量在堆中的对象中),基本类型和引用类型的成员变量都在这个对象的空间中,作为一个整体存储在堆。而类的方法却是该类的所有对象共享的,只有一套,对象使用方法的时候方法才被压入栈,方法不使用则不占用内存。
此外对JVM内存模型中的本地方法栈和方法区做下说明,虽然两个空间都包含"方法"两个字。本地方法栈和虚拟机栈所发挥的作用非常相似,区别是: 虚拟机栈为虚拟机执行 Java 方法 (也就是字节码)服务,而本地方法栈则为虚拟机使用到的 Native 方法服务。 而方法区与 Java 堆一样,是各个线程共享的内存区域,它用于存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据。引用一段英文描述:
The thread stack also contains all local variables for each method being executed (all methods on the call stack). A thread can only access it's own thread stack. Local variables created by a thread are invisible to all other threads than the thread who created it. Even if two threads are executing the exact same code, the two threads will still create the local variables of that code in each their own thread stack. Thus, each thread has its own version of each local variable.
All local variables of primitive types ( boolean, byte, short, char, int, long, float, double) are fully stored on the thread stack and are thus not visible to other threads. One thread may pass a copy of a pritimive variable to another thread, but it cannot share the primitive local variable itself.
The heap contains all objects created in your Java application, regardless of what thread created the object. This includes the object versions of the primitive types (e.g. Byte, Integer, Long etc.). It does not matter if an object was created and assigned to a local variable, or created as a member variable of another object, the object is still stored on the heap.