ExecutorService线程很慢

问题描述 投票:0回答:1

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

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

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

这是该程序的代码:

Class Main:

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();
    }
}

Class 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();
                }
            }
        }
    }
}

Class 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;
    }
}
java multithreading http-proxy
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.