How to continue a for loop after a certain time without using the `try ... catch` method?

new Thread(new Runnable() {
    @Override
    public void run() {
        List<Socket> socketList = new ArrayList<>();
        for (String ip: iplist) {
            Socket socket = null;
            try {
                socket = new Socket(ip,23);
            } catch (IOException e) {
                e.printStackTrace();
            }
            socketList.add(socket);
        }
    }
}).start();

I am trying to create a new socket for each ip in iplist and add this socket to the list sockets. For this, I use a for loop. In use try ... catch, loop execution is suspended for some time when the socket connection fails. So I need a better alternative to know that the socket connection failed and force the loop to continue execution by adding a null socket to the socket list.

+4
source share
1 answer

You can do it:

, , - , . , , , .

, , , , .

- , , . , .

+4

Source: https://habr.com/ru/post/1664635/


All Articles