Failing faster when URL content is not found, howto
        Posted  
        
            by 
                Jam
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jam
        
        
        
        Published on 2014-08-21T16:17:10Z
        Indexed on 
            2014/08/21
            16:20 UTC
        
        
        Read the original article
        Hit count: 246
        
java
|multithreading
I have a thread pool that loops over a bunch of pages and checks to see if some string is there or not. If String is found, or not found response is near instant, however if server is offline or application is not running getting a rejection seems to take seconds
How can I change my code to fail faster?
    for (Thread thread : pool) {
        thread.start();
    }
    for (Thread thread : pool) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
Here is my run method
@Override
public void run() {
    for (Box b : boxes) {
        try {
            connection = new URL(b.getUrl()).openConnection();
            scanner = new Scanner(connection.getInputStream());
            scanner.useDelimiter("\\Z");
            content = scanner.next();
            if (content.equals("YES")) {
            } else {
                System.out.println("\tFAILED ON " + b.getName() + " BAD APPLICATION STATE");
            }
        } catch (Exception ex) {
            System.out.println("\tFAILED ON " + b.getName() + " BAD APPLICATION STATE");
        }
    }
}
© Stack Overflow or respective owner