请教关于static的“牛角尖问题”

紫黑蓝白 2004-04-23 01:40:12
用static 声明的变量,会在多个实例中共享。
如果,分别启动了两个进程,那这两个进程中的同一个static 字段的变量,是否是占用同一个内存地址(即值总是相同的)。
又如果,在两个不同的JAVA虚拟机里运行程序,又会是否相同呢??

请高手指教!
...全文
62 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
紫黑蓝白 2004-04-23
  • 打赏
  • 举报
回复
看看小弟这第三版的程序:
------------------------------------------------
import java.io.*;
public class Yu extends Thread{
//////////////////////////////
public static int tag=10;
//////////////////////////////
public String owner="owner";
public Yu(String owner){
if(owner!=null)
this.owner=owner;
///////////////
//tag=10;
///////////////
}
public void run(){
int i=0;
while(i<30){
System.out.println(this.owner+" - "+tag);
try{
Thread.sleep(500);
}catch(InterruptedException ie){}
++i;
++tag;
}
}
public static void main(String[] args){

Thread t1=new Yu("t1");
t1.start();

try{
Thread.sleep(2000);
}catch(InterruptedException ie){
ie.printStackTrace();
}

Thread t2=new Yu("t2");
t2.start();
}
}
-----------------------------------------------------------
运行结果:
t1 - 10
t1 - 11
t1 - 12
t1 - 13
t2 - 13
t1 - 14
t2 - 15
t1 - 16
t2 - 17
t1 - 18
t2 - 19
t1 - 20
t2 - 21
t1 - 22
t2 - 23
t1 - 24
t2 - 25
t1 - 26
t2 - 27
t1 - 28
t2 - 29
t1 - 30
t2 - 31
t1 - 32
t2 - 33
t1 - 34
t2 - 35
t1 - 36
t2 - 37
t1 - 38
t2 - 39
t1 - 40
t2 - 41
t1 - 42
t2 - 43
t1 - 44
t2 - 45
t1 - 46
t2 - 47
t1 - 48
t2 - 49
t1 - 50
t2 - 51
t1 - 52
t2 - 53
t1 - 54
t2 - 55
t1 - 56
t2 - 57
t1 - 58
t2 - 59
t1 - 60
t2 - 61
t1 - 62
t2 - 63
t1 - 64
t2 - 65
t2 - 67
t2 - 68
t2 - 69
--------------------------------------------------
这回比较清晰点了。。。回去再翻翻thinking in java...呵呵
紫黑蓝白 2004-04-23
  • 打赏
  • 举报
回复
注意:一开始时出现的那行 t2 -10 是因为语句 new Yu("t2") ,它执行了构造器!

对于线程t1 它的tag值由原来的13变回了11; 逐渐递增11,13,15,17
同时另一线程t2 也交替获得执行 12,14,16,

这应该可看出它们共享内存区。 但这小程序对于static变量只会在类首次加载时被初始化一次这一点倒是没能体现出来。。

yongfans 2004-04-23
  • 打赏
  • 举报
回复
static变量是在静态区存储的,它属于类属性,只能初始化一次,虽然你用的是两个
线程,但它们共享同一个变量,所以t2首次调用t2-10。
虚拟机只是把你编写的代码转化成机器识别的代码,我想和它应该没关系。
alpa 2004-04-23
  • 打赏
  • 举报
回复
哈哈,有意思!!!
onefox 2004-04-23
  • 打赏
  • 举报
回复
不同 Java 程序分别启动不同 JVM ,

当然那2个 Static 变量也不有不同的归属,

2者不相通,这点在单利模式里也需要特别注意

qiuwanbin 2004-04-23
  • 打赏
  • 举报
回复
上面这个例子还是不能说明占两个坑啊。
你看后面就知道了,t1,t2的tag是累加的,
至于第5个又从10开始,表明,每new一个新对象时,static属性都会被重新初始化。
-----
两个虚拟机?不知道。不过我想当然地认为,它们管理不同的内存(当然会有不同的值)。
紫黑蓝白 2004-04-23
  • 打赏
  • 举报
回复
我不小新搞错了。。。这论坛不能连续发3次回贴,也不好改,呵呵。

------------------------------------------------------------
public void run(){
int i=0;
while(i<30){
System.out.println(this.owner+" - "+tag);
try{
Thread.sleep(500);
}catch(InterruptedException ie){}
++i;
++tag;
}
}

-----------------------------------------------------------
结果成了这样。呵呵:

t1 - 10
t1 - 11
t1 - 12
t1 - 13
t2 - 10
t1 - 11
t2 - 12
t1 - 13
t2 - 14
t1 - 15
t2 - 16
t1 - 17
t2 - 18
t1 - 19
t2 - 20
t1 - 21
t2 - 22
t1 - 23
t2 - 24
t1 - 25
t2 - 26
t1 - 27
t2 - 28
t1 - 29
t2 - 30
t1 - 31
t2 - 32
t1 - 33
t2 - 34
t1 - 35
t2 - 36
t1 - 37
t2 - 38
t1 - 39
t2 - 40
t1 - 41
t2 - 42
t1 - 43
t2 - 44
t1 - 45
t2 - 46
t1 - 47
t2 - 48
t1 - 49
t2 - 50
t1 - 51
t2 - 52
t1 - 53
t2 - 54
t1 - 55
t2 - 56
t1 - 57
t2 - 58
t1 - 59
t2 - 60
t1 - 61
t2 - 62
t2 - 64
t2 - 65
t2 - 66


--------------------------------------------
哪位高手先帮帮小弟这问题啊:

如果,分别启动了两个进程,那这两个进程中的同一个static 字段的变量,是否是占用同一个内存地址(即值总是相同的)。
又如果,在两个不同的JAVA虚拟机里运行程序,又会是否相同呢??

qiuwanbin 2004-04-23
  • 打赏
  • 举报
回复
倒,你还真会钻。
在不同的程序中你都
tag=++i;了
当然看起来象是不同的。
这并不能说明它们在不同内存里。
只能说,这两个i是不同的。
紫黑蓝白 2004-04-23
  • 打赏
  • 举报
回复
这是上面的代码运行结果:
------------------------------------------------------
t1 - 10
t1 - 1
t1 - 2
t1 - 3
t2 - 10
t1 - 4
t2 - 1
t1 - 5
t2 - 2
t1 - 6
t2 - 3
t1 - 7
t2 - 4
t1 - 8
t2 - 5
t1 - 9
t2 - 6
t1 - 10
t2 - 7
t1 - 11
t2 - 8
t1 - 12
t2 - 9
t1 - 13
t2 - 10
t1 - 14
t2 - 11
t1 - 15
t2 - 12
t1 - 16
t2 - 13
t1 - 17
t2 - 14
t1 - 18
t2 - 15
t1 - 19
t1 - 20
t2 - 16
t1 - 21
t2 - 17
t1 - 22
t2 - 18
t1 - 23
t2 - 19
t1 - 24
t2 - 20
t1 - 25
t2 - 21
t1 - 26
t2 - 22
t1 - 27
t2 - 23
t1 - 28
t2 - 24
t1 - 29
t2 - 25
t2 - 26
t2 - 27
t2 - 28
t2 - 29
紫黑蓝白 2004-04-23
  • 打赏
  • 举报
回复
把这段代码运行下,看和你想想的有什么不同哦。呵呵。
--------------------------------------------------
public class Yu extends Thread{
public static int tag=0;
public String owner="owner";
public Yu(String owner){
if(owner!=null)
this.owner=owner;
tag=10;
}
public void run(){
int i=0;
while(i<30){
System.out.println(this.owner+" - "+tag);
try{
Thread.sleep(600);
}catch(InterruptedException ie){}
tag=++i;
}
}
public static void main(String[] args){
Thread t1=new Yu("t1");
t1.start();

try{
Thread.sleep(2000);
}catch(InterruptedException ie){
ie.printStackTrace();
}

Thread t2=new Yu("t2");
t2.start();
}
}
-----------------------------------------------------
似乎 tag 变成了独立的变量,在t1和t2中值不是同一个。。仅仅是类加载只一次?!,第二次是第一次的拷贝。
紫黑蓝白 2004-04-23
  • 打赏
  • 举报
回复
那多个虚拟机间应该不同的吧,好象,不同虚拟机之间的资源是独立的。
随便问一下,在同一台机子上会跑两个以上的虚拟机吗?
Arias 2004-04-23
  • 打赏
  • 举报
回复
应该是的呀!
static 型的声明它就是为了避免拷贝多个而出现的!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧