运行同一个 java 程序两次,类中包含静态变量
(我不知道是否应该在这里问这个问题) 我想运行同一个java程序两次,其中大部分变量静态。
如果我运行两次(同时)这些静态变量是否具有相同的值或不同的值?
由于静态变量是类变量,这让我感到困惑。
(I do not know whether i should ask this here or not)
I want to run the same java program twice which has most of the variables static.
if i ran this twice(concurrently) will these static variables have same values or different?
As the static variables are class variables, this is confusing me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态值是针对每个类的,如果您运行同一个 java 程序两次,那么它将创建两个 JVM(您将在任务列表中看到两个 java 进程),这将加载该类两次(每个 JVM 一次),因此静态变量不会被共享。
The static values are per-class and if you run the same java program twice, then it will create two JVMs (you'll see two java processes in the task list), which will load the class twice (once per JVM), so the static variables will not be shared.
每次运行都会有自己的 JVM 实例,因此静态变量不会发生冲突。
Each run will have its own JVM instance so static variables will not collide.
如果您启动应用程序两次,例如,通过:
那么第二次运行的值将不会受到第一次运行的影响。这是一个全新的过程。
但是,如果您在单个应用程序的生命周期中运行静态方法两次,那么我上面写的内容就不再成立了。
If you are starting the application two times, for example, via:
Then the values for the 2nd run will not be influenced by the first run. It's an entirely new process.
If, however you are running a static method two times, in the lifecycle of a single application, then, what I wrote above doesn't hold anymore.