java静态变量和进程 [英] java static variable and process

查看:126
本文介绍了java静态变量和进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明一个变量static并将此类编译成可执行jar。如果我使用这样的批处理文件开始这个类:

If I declare a variable static and compiled this class into a executable jar. If I start this class using batch file like this:

java MyClass
java MyClass
java MyClass

所有3个进程是否会共享同一个变量?

Would all 3 process share the same variable?

推荐答案

不。静态变量特定于JVM实例。事实上 - 它特定于加载类的类加载器。因此,如果你创建了三个独立的类加载器,每个加载器负责加载 MyClass (不只是委托给一些普通父类),它们每个都有一个单独的,独立的静态变量 MyClass

No. The static variable is specific to the JVM instance. More than that, in fact - it's specific to the class loader which loads the class. So if you created three separate class loaders, each responsible for loading MyClass (not just delegating to some common parent) they'd each have a separate, independent static variable in MyClass.

这篇关于java静态变量和进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆