Java 在公共void方法()内的for循环中创建新线程
我有一个ping IP地址的类。要开始ping,我有公共vod run()方法来开始ping。问题是我希望同时ping更多的IP地址(对于每个IP地址,我需要新线程)。那么我如何在for循环中创建新线程呢。以下是我的ping类代码:Java 在公共void方法()内的for循环中创建新线程,java,multithreading,thread-safety,ping,void,Java,Multithreading,Thread Safety,Ping,Void,我有一个ping IP地址的类。要开始ping,我有公共vod run()方法来开始ping。问题是我希望同时ping更多的IP地址(对于每个IP地址,我需要新线程)。那么我如何在for循环中创建新线程呢。以下是我的ping类代码: public void run() { if (dbConnection.ConnectToDB()) { for (;GateWayKey<=GateWayKeyStop;GateWayKey++
public void run()
{
if (dbConnection.ConnectToDB())
{
for (;GateWayKey<=GateWayKeyStop;GateWayKey++)
{
if(stop || this.isInterrupted()){
return;
}
ip="192.168."+GateWayKey+".1";
InetAddress address;
try {
address = InetAddress.getByName(ip);
try {
if (address.isReachable(PingTime))
{
//System.out.println("Pronaden GateWay: "+ip)
// labele.IP
sql="INSERT INTO `iptables` (`IP` , `COMPUTER_NAME` , `GATEWAY_KEY`) VALUES ('"+ip+"', '"+address.getHostName().toString()+"', '"+GateWayKey+"');";
framedocs.WriteMonitorData (ip, address.getHostName().toString(),"2000","DA",dbConnection.WriteToDB(sql));
for (;SubNetKey<=SubNetKeyStop;SubNetKey++)
{
if(stop || this.isInterrupted()){
return;
}
InetAddress addressIps = InetAddress.getByName("192.168."+GateWayKey+"."+SubNetKey);
System.out.println("Provjeravam IP: "+addressIps);
if (addressIps.isReachable(PingTime))
{
ip="192.168."+GateWayKey+"."+SubNetKey;
System.out.println("Pronaden IP: "+ip);
sql="INSERT INTO `iptables` (`IP` , `COMPUTER_NAME` , `GATEWAY_KEY`) VALUES ('"+ip+"', '"+addressIps.getHostName().toString()+"', '"+GateWayKey+"');";
framedocs.WriteMonitorData (ip, address.getHostName().toString(),"2000","DA",dbConnection.WriteToDB(sql));
}
else
{
framedocs.WriteMonitorData (addressIps.toString(), "N/A","2000","NE","N/A");
}
}
}
else
{
framedocs.WriteMonitorData (ip, "N/A","2000","NE","N/A");
}
} catch (IOException e) {
// TODO Auto-generated catch block
framedocs.WriteMonitorData (ip, "N/A","2000",e.getMessage(),"N/A");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
;
framedocs.WriteMonitorData (ip, "N/A","2000",e.getMessage(),"N/A");
}
}
}
else
{
framedocs.WriteMonitorData ("MySQL error", "N/A","N/A","N/A","N/A");
}
public void run()
{
if(dbConnection.ConnectToDB())
{
对于(;GateWayKey在主类中创建一个新类,并在该内部类中执行操作
每次需要创建新线程时,只需启动该内部类的一个新实例,并调用为此目的创建的方法
如果您觉得这个答案没有用,请查看我关于多线程的另一个答案。执行这类任务的一般方法是,首先,创建一个类来保存希望从每个线程获得的结果:
final class PingResult {
public String ip;
public String hostname;
//... other things you want go here
}
然后创建一个执行实际工作的callable
class PingTask extends Callable<PingResult>{
private final String gateWayKey, subNetKey;
//... other parameters you need go here
public Ping( String gwKey, String snKey /*+ others? */ ){
// This is just a way to pass parameters to your pinging function
this.gateWayKey = gwKey;
this.subNetKey = snKey;
// ...
}
public PingResult call(){
// Do actual pinging work here
if ( /* Success */ )
{
PingResult result = new PingResult();
result.ip= /*Fill these in*/;
result.hostname = /* ... */;
return result;
}
// Otherwise we failed, I'm using null as a failure sentinel
// (you can come up with something better)
return null;
}
}
类PingTask扩展了可调用的{
私有最终字符串网关密钥,子网密钥;
//…您需要的其他参数在这里
公共Ping(字符串gwKey、字符串snKey/*+其他?*/){
//这只是将参数传递给ping函数的一种方法
this.gateWayKey=gwKey;
this.subNetKey=snKey;
// ...
}
公共PingResult调用(){
//在这里做实际的ping工作
如果(/*成功*/)
{
PingResult=新的PingResult();
result.ip=/*将这些填入*/;
result.hostname=/*…*/;
返回结果;
}
//否则我们就失败了,我用null作为失败哨兵
//(你可以想出更好的办法)
返回null;
}
}
然后在调用代码中,设置线程池,提示请求,然后处理结果
// Might need to tweak the # for best performance
final int NUM_THREADS = Runtime.getRuntime.availableProcesses();
ExecutorService exec = Executors.newFixedThreadPool( NUM_THREADS );
List<Future<PingResult>> results = new ArrayList<PingResult>();
for(/* ... */){
results.add( exec.submit( new PingTask( gateway, subnet ) ) );
}
for( Future<PingResult> future : results ){
PingResult result = future.get();
// Process the result here (this is where you insert into the DB)
}
exec.shutdown(); // VERY IMPORTANT. If you don't do this the JVM will never stop.
//可能需要调整#以获得最佳性能
final int NUM_THREADS=Runtime.getRuntime.availableprocesss();
ExecutorService exec=Executors.newFixedThreadPool(NUM_线程);
列表结果=新建ArrayList();
对于(/*…*/){
添加(exec.submit(新PingTask(网关、子网));
}
用于(未来:结果){
PingResult结果=future.get();
//在此处处理结果(在此处插入数据库)
}
exec.shutdown();//非常重要。如果不这样做,JVM将永远不会停止。
您尝试了什么?您阅读了吗?您可能想使用线程池。无论如何,对于正在体验线程的人来说,在For中创建线程应该不会是一个问题。我尝试在public void函数中创建线程,并且在每个循环中创建新的线程实例并在最后运行。但是ofc。这没有帮助:(哦,好主意。我会试试看的