日期:2014-05-17  浏览次数:20678 次

折磨了几天的多线程问题 求大神
本帖最后由 ml774032194 于 2013-01-07 11:15:35 编辑
多线程访问多个服务器并下载一个文件 并且跳转到一个页面显示下载结果
现在是在windows上程序是能正常运行的,但是我把项目部署到Linux服务器上时 出现了问题
下面是代码:

public class TestThread implements Runnable {
public Serverproxy serverproxy;
private TestITDSDownload ts;
public TestThread(Serverproxy serverproxy,TestITDSDownload tsd) {
this.serverproxy = serverproxy;
this.ts=tsd;
}

public void run() {
try {
int result=ts.getITDSStatus(serverproxy.getIp(),                                                                                                             
                                serrverproxy.getFileId());//下载的方法会返回一个int类型判断是否下载成功
if(result==1)
{serverproxy.setResult("下载成功");}
if(result==2)
{serverproxy.setResult("下载失败");}
if(result==4)
{serverproxy.setResult("内部错误");}      System.out.println("serverproxyResult:"+serverproxy.getIp()+serverproxy.getResult());
System.out.println("size为:"+TestITDSDownload.getSize());
System.out.println("结果添加前的serverproxyCheckList的长度:"+ts.serverproxyCheckList.size());
ts.serverproxyCheckList.add(serverproxy);
System.out.println("结果添加后的serverproxyCheckList的长度:"+ts.serverproxyCheckList.size());
System.out.println("现在的isSuccess为:"+ts.getSuccess());
if(ts.serverproxyCheckList.size()==TestITDSDownload.getSize()){
ts.setSuccess(true);
System.out.println("相等,isSuccess为:"+ts.getSuccess());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}

serverproxy:服务器对象 保存的是服务器的IP,端口和下载结果
线程里访问传进来的服务器,然后去下载内容 并将下载结果 添加到对应的serverproxy对象中

public  class TestITDSDownload  {
public  List<Serverproxy> serverproxyCheckList = new ArrayList<Serverproxy>();
public  boolean isSuccess=false;
public boolean getSuccess() {
return isSuccess;
}
public void setSuccess(boolean isSuccess) {
this.isSuccess = isSuccess;
}
//..其他代码
//开启线程的方法
public List<Serverproxy> check(List<Serverproxy> serverproxyList) throws Exception {
TestITDSDownload.setSize(serverproxyList.size());
for (int i = 0; i < serverproxyList.size(); i++) {
TestThread t = new TestThread(serverproxyList.get(i),this);
Thread thread = new Thread(t);
thread.start();
}
while(true){
if(getSuccess()){
System.out.println("成功!当前isSuccess为:"+getSuccess());
break;
}
}
return serverproxyCheckList;