当前位置: 代码迷 >> Java相关 >> 线程中的问题
  详细解决方案

线程中的问题

热度:143   发布时间:2007-05-18 22:44:46.0
线程中的问题

import java.util.Vector;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import sun.net.ftp.FtpClient;
import java.io.InputStream;
import java.awt.Color;
import java.io.BufferedReader;
import java.util.HashMap;


public class ConnectTest {

Vector threadVector = new Vector();
Thread t1;

StringBuffer strAllInfo;//得到所有的错误信息

ConnectTest(Vector vector, boolean bUseMail, boolean bUseSound,
boolean bUseMsg, String mailADD, String mailPWD) {

for (int i = 0; i < vector.size(); i++) {
t1 = new Thread(new ThreadFtpConn((String) vector.get(i), bUseMail,
bUseSound, bUseMsg, mailADD,
mailPWD));
t1.start();
threadVector.addElement(t1);

}
}

public void stop() {
Thread t1;
for (int j = 0; j < threadVector.size(); j++) {
t1 = (Thread) threadVector.get(j);
t1.stop();
}
System.out.println("Ftp线程已停止");
}

public static void main(String[] args) {
// ConnectTest connecttest = new ConnectTest();ghfghfg
Vector vector = new Vector();
vector.addElement("10.2.0.4:21:5");
vector.addElement("10.1.9.6:21:4");
vector.addElement("10.1.9.100:21:5");
vector.addElement("10.1.9.4:21:7");
ConnectTest CT = new ConnectTest(vector, true, true, true, "aa@", "PWD");
}
}


class ThreadFtpConn implements Runnable {
PopFrameDemo ftPopFrame = new PopFrameDemo(); //提示框
//static StringBuffer strInfo;

private String strFtpIpPort = "";
private String strFtpIp = "";
private int intFtpPort = 0;
private int intFtpSleep = 0;

private boolean msgAlarm = false;
private boolean mailAlarm = false;
private boolean soundAlarm = false;
private String mailADD = "";
private String mailPWD = "";

ThreadFtpConn(String strFtpIpPort, boolean bUseMail, boolean bUseSound,
boolean bUseMsg, String mailADD, String mailPWD) {
this.strFtpIpPort = strFtpIpPort;
this.setFtpIp(strFtpIpPort);
this.setFtpPort(strFtpIpPort);
this.setFtpSleep(strFtpIpPort);
this.mailAlarm = bUseMail;
this.msgAlarm = bUseMsg;
this.soundAlarm = bUseSound;
this.mailADD = mailADD;
this.mailPWD = mailPWD;
System.out.println(mailAlarm);
System.out.println(msgAlarm);
System.out.println(soundAlarm);
System.out.println(mailADD);
System.out.println(mailPWD);
}

/*
*
*
*/
private void setFtpIp(String strFtpIpPort) {

int intFirstPosition = strFtpIpPort.indexOf(":");
String strFtpIp = strFtpIpPort.substring(0, intFirstPosition);
this.strFtpIp = strFtpIp;
}

/*
*
*
*/
private void setFtpPort(String strFtpIpPort) {
int intFirstPosition = strFtpIpPort.indexOf(":");
int intLastPosition = strFtpIpPort.lastIndexOf(":");
int intFtpPort = Integer.parseInt(strFtpIpPort.substring(
intFirstPosition + 1,
intLastPosition));
this.intFtpPort = intFtpPort;
}

/*
*
*
*/
private void setFtpSleep(String strFtpIpPort) {
int intLastPosition = strFtpIpPort.lastIndexOf(":");
String strFtpSleep = strFtpIpPort.substring(intLastPosition + 1);
int intFtpSleep = Integer.parseInt(strFtpSleep);
this.intFtpSleep = intFtpSleep;

}

/*
*
*
*/
private boolean IsConnect(String ip, int port) {
String strInfo="";
try {
Socket socket = new Socket(ip, port);
return true;
} catch (UnknownHostException e) {
System.out.println("错误的IP地址");
} catch (IOException e) {
strInfo="不能通过端口 " + port + " 与主机 " + ip + " 进行FTP连接";
System.out.println(strInfo);


}
return false;
}

private boolean IsFtpConnect(String ip) {
FtpClient ftpclient = new FtpClient();

try {
ftpclient.openServer(ip);
ftpclient.login("anonymous", "anonymous");
InputStream is = ftpclient.list();
StringBuffer info = new StringBuffer();
int ch;
while ((ch = is.read()) >= 0) {
info.append((char) ch);
}
//System.out.println(new String(info));
is.close();
return true;
} catch (IOException e) {
System.out.println();
System.out.println("主机 " + ip + " FTP服务没有开启或者出现异常");
}

return false;
}


public void run() {
while (true) {
if (this.IsConnect(strFtpIp, intFtpPort)) {
if (this.IsFtpConnect(strFtpIp)) {
System.out.println("ftp " + strFtpIp + " 正常运行");
} else {
System.out.println("ftp " + strFtpIp + " no connect");
}
} else {

// System.out.println("connect error!");

}
try {
Thread.sleep(intFtpSleep * 1000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}

}
}



问题:我想用strAllInfo保存所有线程的strInfo的值,该怎么做..

求求各位了.帮忙看看该怎么做.实在想不出办法了..谢谢啦.

搜索更多相关的解决方案: 线程  

----------------解决方案--------------------------------------------------------
啊刚回复了没保存 没给发出去
如果想存储的是strInfo="不能通过端口 " + port + " 与主机 " + ip + " 进行FTP连接";

可以用Vector 也可以用LinkList 定义LinkList<Thread> listthread存储t1;定义LinkList<String> liststring存储strInfo ;
在这段代码
t1 = new Thread(new ThreadFtpConn((String) vector.get(i), bUseMail,
bUseSound, bUseMsg, mailADD,
mailPWD));
t1.start();
加上listthread.add(t1);
liststring.add(strInfo);
一一对应;
如果正常会把上一次线程错误信息存入 要在run的try{
语句中加如strInfof="true";
在要输出str
Info的时候区别 不输出来
先get线程当前的index然后输出liststring.(i);
我不能肯定好使不好使 楼主试试吧先

----------------解决方案--------------------------------------------------------

十分感谢楼上的回复.我去试试.


----------------解决方案--------------------------------------------------------
再次感谢2楼的.我用其他方法解决了.谢谢你的帮助..
----------------解决方案--------------------------------------------------------

呵呵 不会是用的BufferString吧 那个是按字节存储的


----------------解决方案--------------------------------------------------------
  相关解决方案