Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于多线程访问静态方法的问题
hganbo





发贴: 2
积分: 0
于 2004-12-15 17:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟最近在做一个项目遇到一点线程同步的问题,还没有找出具体的问题。
怀疑问题出在多线程访问静态方法上,但没有找到有相关资料讲这方面的问题,所以只好请教一下各位java高手了。

我的原来程序是这样设计的,对于一些常用的方法,都用静态方法来实现,在多线程程序中直接调用,静态方法由于没有使用静态变量,所以没有进行线程同步。

类似以下程序:

class ThreadI {
public static void main(String[] arg) {

TestI p = new TestI("thread 1");
new TestI("thread 2").start();
new TestI("thread 3").start();
new TestI("thread 4").start();
new TestI("thread 5").start();
new TestI("thread 6").start();
new TestI("thread 7").start();
new TestI("thread 8").start();
new TestI("thread 9").start();
new TestI("thread 10").start();
new TestI("thread 11").start();
new TestI("thread 12").start();
new TestI("thread 13").start();
new TestI("thread 14").start();
new TestI("thread 15").start();
new TestI("thread 16").start();
new TestI("thread 17").start();
new TestI("thread 18").start();
new TestI("thread 19").start();
new TestI("thread 20").start();
p.start();
}
}

class TestI extends Thread {
String s ;
TestI(String sTemp){
this.s = sTemp;
}
public void run() {
try{

Thread.currentThread().sleep( 1000*5);
}catch(Exception ex){}

for(int i=0;i<100;i++){
System.out.println(T.t( this.s + " "+ i));
}
}
}

class T{
public static String t(String sTemp){
String aa = sTemp;
return aa;
}
}

从上面的例子程序打出的结果,我看不出有线程访问冲突的地方。

我的疑问是:对于T.t这种静态方法,在不同线程调用时,jvm究竟是怎么进行处理的?
我自己的理解是对于这样的静态方法是先实例化成各线程堆栈内的具体方法,然后再进行运算,由于都是局部变量,没有使用静态变量,各线程之间是不存在资源冲突的。但没有找到资料来证实这样的理解,所以比较烦恼。还请各位帮忙解决一下。




话题树型展开
人气 标题 作者 字数 发贴时间
22055 关于多线程访问静态方法的问题 hganbo 1449 2004-12-15 17:21
17783 Re:关于多线程访问静态方法的问题 tool 95 2004-12-31 07:47
17667 Re:关于多线程访问静态方法的问题 bwpc 46 2004-12-31 11:01
17703 Re:关于多线程访问静态方法的问题 colo007 7 2005-01-10 18:52
18104 Re:关于多线程访问静态方法的问题 bloodwolf 246 2005-01-13 15:58
17604 Re:关于多线程访问静态方法的问题 bwpc 187 2005-01-13 17:10
17730 Re:关于多线程访问静态方法的问题 hganbo 663 2005-01-17 14:18

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923