首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

不可思议的结果。哪位高手能帮忙解释下。

2012-01-20 
不可思议的结果。。。谁能帮忙解释下。。。运行结果:线程1开始:帮忙a长江长江bag东方country黄河线程2开始:黄河c

不可思议的结果。。。谁能帮忙解释下。。。
运行结果:
线程1开始:帮忙     a   长江     长江   bag   东方   country   黄河  
线程2开始:黄河   country   黄河   country   可能   hello  
线程3开始:   可能   hello   你好   is   this   我     this   where   阳光  
线程4开始:阳光     where   women   字典   字典   women  
线程1结束:str_PartText[0]=帮忙     a   长江   bag   东方   country   黄河  
线程2结束:str_PartText[1]=黄河   country   可能   hello  
线程3结束:str_PartText[2]=   可能   hello   你好   is   this   我   where   阳光  
线程4结束:str_PartText[3]=阳光     where   women   字典  
线程结束
false
false
false
false
int_temp   is   :4
线程1开始:帮忙     a   长江   bag   东方   country   黄河   黄河   country   可能   hello  
线程2开始:   可能   hello   你好   is   this   我   where   阳光   阳光     where   women   字典  
线程2结束:str_PartText[1]=   可能   hello   你好   is   this   我   where   阳光   women   字典  
线程1结束:str_PartText[0]=帮忙     a   长江   bag   东方   country   黄河   可能   hello  
线程结束
false
false
int_temp   is   :2
线程1开始:帮忙     a   长江   bag   东方   country   黄河   可能   hello   帮忙     a   长江   bag   东方   country   黄河   可能   hello  
线程1结束:str_PartText[0]=帮忙     a   长江   bag   东方   country   黄河   可能   hello  
线程结束
false
int_temp   is   :1
排序结果为:
-------------------------
帮忙     a   长江   bag   东方   country   黄河   可能  
hello  


run方法为:
public   void   run(){
if(Thread.currentThread()==thr_StringSort[0]){
String   string_guibing=str_PartText[0]+str_PartText[1];
System.out.println( "线程1开始: "+string_guibing);
try{
Thread.sleep(10);
}catch(InterruptedException   e){
e.printStackTrace();
}
string_guibing=PaiChong(string_guibing);
str_PartText[0]=Sort(string_guibing);
System.out.println( "线程1结束:str_PartText[0]= "+str_PartText[0]);
}
if(Thread.currentThread()==thr_StringSort[1]){
String   string_guibing=str_PartText[2]+str_PartText[3];
System.out.println( "线程2开始: "+string_guibing);
try{
Thread.sleep(10);
}catch(InterruptedException   e){
e.printStackTrace();
}
string_guibing=PaiChong(string_guibing);
str_PartText[1]=Sort(string_guibing);
System.out.println( "线程2结束:str_PartText[1]= "+str_PartText[1]);
}
if(Thread.currentThread()==thr_StringSort[2]){
String   string_guibing=str_PartText[4]+str_PartText[5];
System.out.println( "线程3开始: "+string_guibing);
try{
Thread.sleep(10);
}catch(InterruptedException   e){
e.printStackTrace();
}
string_guibing=PaiChong(string_guibing);
str_PartText[2]=Sort(string_guibing);
System.out.println( "线程3结束:str_PartText[2]= "+str_PartText[2]);
}
if(Thread.currentThread()==thr_StringSort[3]){
String   string_guibing=str_PartText[6]+str_PartText[7];
System.out.println( "线程4开始: "+string_guibing);
try{
Thread.sleep(10);


}catch(InterruptedException   e){
e.printStackTrace();
}
string_guibing=PaiChong(string_guibing);
str_PartText[3]=Sort(string_guibing);
System.out.println( "线程4结束:str_PartText[3]= "+str_PartText[3]);
}
if(Thread.currentThread()==thr_StringSort[4]){
String   string_guibing=str_PartText[8]+str_PartText[9];
string_guibing=PaiChong(string_guibing);
str_PartText[4]=Sort(string_guibing);
}
}

[解决办法]
没看懂~
[解决办法]
没看懂~too
[解决办法]
同上。。。。。
[解决办法]
代码不全,难以定位。
注意:PaiChong(string_guibing);
Sort(string_guibing);
两个方法中不要修改同共同的变量,除非增加了同步控制。
[解决办法]
同意楼上的意见
还有,4个线程一起运行,又没有什么限制,那就是让它们在抢了,再加上上面的原因,这样结果肯定是很乱
[解决办法]
这个程序很有意思,
[解决办法]
看一下帮助里面线程关闭的内容吧!

热点排行