gpt4 book ai didi

java - ExecutorService线程很慢

转载 作者:行者123 更新时间:2023-12-01 18:38:20 27 4
gpt4 key购买 nike

因此,我使用ExecutorService进行多线程处理,并且由于任务主要是代理检查,因此线程数很高(大约1000个线程)。好吧,当没有添加线程但它们被缓慢杀死时,应用程序开始变慢,线程被杀死的速度越来越慢,直到它停止并挂起少量线程(例如25个线程)为止。

如何解决这些问题而又不实际强行杀死这些线程并完成工作呢?

编辑:
早上好,
我已经记录了我的问题,以便我们所有人都可以理解,即使我误解了。 https://streamable.com/cgoa6

这是该程序的代码:

主班:

package me.inao.proxycheck;

import me.inao.proxycheck.menu.Selection;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
public static void main(String[] args){
byte mode = new Selection().getNumSelect(new String[]{"Proxy check", "Test2", "Test3"});
byte proxyType = 0;
BufferedReader reader = null;
int threads = 1000;
ExecutorService pool = Executors.newFixedThreadPool(threads);
if(mode == 1){
proxyType = new Selection().getNumSelect(new String[]{"HTTP(s)", "SOCKS"});
try{
reader = new BufferedReader(new FileReader("./proxies.txt"));
String line;
boolean breakx = false;
while(true){
for(int i = 0; i < (threads+1); i++){
line = reader.readLine();
byte finalProxyType = proxyType;
String finalLine = line;
if(finalLine != null){
pool.execute(() -> {
new Thread().checkProxy(finalLine, finalProxyType);
});

}else{
breakx = true;
break;
}
}
if(breakx){
break;
}
}
}catch (Exception e){
System.out.println("File 'proxies.txt.txt' not found..");
}
}
pool.shutdown();
}
}


类me.inao.proxycheck.Thread:

package me.inao.proxycheck;

import me.inao.proxycheck.proxy.ProxyCheck;

import java.io.File;
import java.io.FileWriter;
import java.net.Proxy;

public class Thread extends java.lang.Thread {
public void checkProxy(String line, byte type){
Proxy proxy = new ProxyCheck().createProxy(line.split(":")[0], line.split(":")[1], type);
if(new ProxyCheck().isValid(proxy)){
System.out.println("Found valid: " + proxy.type() + " | " + proxy.address().toString());
if(!(new File("./proxies/").exists())){
new File("./proxies").mkdir();
}
File f;
if(type == 1) f = new File("./proxies/workingHTTP.txt");
else if(type == 2) f = new File("./proxies/workingSOCKS.txt");
else f = new File("./proxies/workingOTHERS.txt");
if(f.exists()){
try{
FileWriter wr = new FileWriter(f, true);
wr.write(line + "\n");
wr.flush();
wr.close();
}catch (Exception e){
e.printStackTrace();
}
}else{
try{
FileWriter wr = new FileWriter(f);
wr.write(line + "\n");
wr.flush();
wr.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}


类me.inao.proxycheck.proxy.ProxyCheck

package me.inao.proxycheck.proxy;

import java.net.*;

public class ProxyCheck {
public Proxy createProxy(String addr, String port, byte type){
if(addr == null || port == null){
System.out.println("Provided invalid proxy (null)");
return null;
}
SocketAddress address = new InetSocketAddress(addr, Integer.parseInt(port));
if(type == 1){
return new Proxy(Proxy.Type.HTTP, address);
}
else if(type == 2){
return new Proxy(Proxy.Type.SOCKS, address);
}
else return null;
}
public boolean isValid(Proxy proxy){
try{
if(proxy != null){
URL url = new URL("http://azenv.net/");
HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.connect();
if(conn.getResponseCode() == 200){
return true;
}else{
return false;
}
}
}catch (Exception e){
return false;
}
return false;
}
}

最佳答案

想要使用ExecutorService service = Executors.newCachedThreadPool()时,听起来好像正在使用ExecutorService service = Executors.newFixedThreadPool(n),其中n是要一直保持打开状态的线程数。这给初始化带来了沉重的处理负担,但是,只要您在整个程序中保持它运行,它就可以极大地帮助您解决问题。

关于java - ExecutorService线程很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com