Java example of multi-thread application

Let’s see example of multi-thread application. For this example we will check which addresses are accessible from our local network. To do that we will require to be provided the subnet of the network local network in format x.x.x . For example 192.168.0 , 192.168.1 , 192.168.100 are some typical subnets. After that the program will check how many core our machine has. Then it will split the number of the addresses and for each core will create Thread with portion of this addresses to be check. The check is done with InetAddress method isReachable. We also provide timeout of 500 milliseconds.

Each thread accept as constructor parameter new instance of SearchIp class which implements Runnable interface.

Let’s see the Main class:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("Start of the Program");

        try (Scanner in = new Scanner(System.in)) {
            System.out.println("Please provide subnet in format x.x.x "
                    +" (for example 192.168.1 or 192.168.0): ");

            String subnet = in.nextLine();

            // Get the available number of cores
            int processorCores = Runtime.getRuntime().availableProcessors();
            System.out.println("Number of processor cores are: " + processorCores);

            int numCheck = (int) Math.ceil((double) 255 / processorCores);

            System.out.println("Each processor core will test: " + numCheck + " addresses");

            // For every processor core create Thread and start SearchIp run method
            for (int i = 0; i < processorCores; i++) {
                int start = i * numCheck;
                int end = i * numCheck + numCheck;
                if (end > 255) {
                    end = 255;
                }
                Thread thread = new Thread(new SearchIp(subnet, start, end));
                thread.start();
            }
        }
    }
}

And here is SearchIp:

import java.io.IOException;
import java.net.InetAddress;

public class SearchIp implements Runnable {
    public static final int TIMEOUT = 500;
    private final String subnet;
    private final int start;
    private final int end;

    public SearchIp(String subnet, int start, int end) {
        this.subnet = subnet;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        try {
            StringBuilder sb = new StringBuilder("Thread address range to check: ");
            sb.append(Thread.currentThread().getName());
            sb.append(" [ ");
            sb.append(subnet);
            sb.append(".");
            sb.append(start);
            sb.append(" -> ");
            sb.append(subnet);
            sb.append(".");
            sb.append(end);
            sb.append(" ]");

            System.out.println(sb);

            // check for every address if it is reachable
            for (int i = start; i < end; i++) {
                String host = subnet + "." + i;
                if (InetAddress.getByName(host).isReachable(TIMEOUT)) {
                    InetAddress address = InetAddress.getByName(host);
                    String hostname = address.getHostName();
                    System.out.println(hostname + " is reachable");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now for the result:

Start of the Program
Please provide subnet in format x.x.x (for example 192.168.1 or 192.168.0): 
192.168.0
Number of processor cores are: 12
Each processor core will test: 22 addresses
Thread address range to check: Thread-0 [ 192.168.0.0 -> 192.168.0.22 ]
Thread address range to check: Thread-1 [ 192.168.0.22 -> 192.168.0.44 ]
Thread address range to check: Thread-2 [ 192.168.0.44 -> 192.168.0.66 ]
Thread address range to check: Thread-3 [ 192.168.0.66 -> 192.168.0.88 ]
Thread address range to check: Thread-4 [ 192.168.0.88 -> 192.168.0.110 ]
Thread address range to check: Thread-5 [ 192.168.0.110 -> 192.168.0.132 ]
Thread address range to check: Thread-6 [ 192.168.0.132 -> 192.168.0.154 ]
Thread address range to check: Thread-7 [ 192.168.0.154 -> 192.168.0.176 ]
Thread address range to check: Thread-8 [ 192.168.0.176 -> 192.168.0.198 ]
Thread address range to check: Thread-9 [ 192.168.0.198 -> 192.168.0.220 ]
Thread address range to check: Thread-10 [ 192.168.0.220 -> 192.168.0.242 ]
Thread address range to check: Thread-11 [ 192.168.0.242 -> 192.168.0.255 ]
192.168.0.1 is reachable
192.168.0.199 is reachable
192.168.0.200 is reachable
192.168.0.47 is reachable
192.168.0.144 is reachable
192.168.0.18 is reachable

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.