Hello everyone,

I have a collection of threads that searches for a number based on the threads. The search is done in a for...loop and if after 10 tries it doesn't find the number it's supposed to let the other threads try and search. This continues until the number is found. Then, once the number is found it's supposed to interrupt the other threads in the collection.

So that's what it's supposed to do... but I can't get it to do that. Here's what I have:


Java Code:
import java.util.ArrayList;
public class Monitor extends Thread
{
    private ArrayList<Thread> threadList = new ArrayList<Thread>();
    private static boolean isFound = false;
    
    public void addThread(Thread t)
    {
        threadList.add(t); //added a thread object: notify the other threads.
    }
    public synchronized void foundNumber()
    {
        while(!isInterrupted()){
        try
        {
           Thread.currentThread().interrupt();
           Thread.currentThread().sleep(100);
        }
        catch(InterruptedException e)
        {
            System.out.println(Thread.currentThread().getName() + " is interrupted");
        }
        
        }
        isFound = true;
    }
    public void run()
    {
        while(!isFound)
        {
           
        }
    }
}

Java Code:
import java.lang.Thread;
import java.lang.Math;

public class NumberFinderThreadApp
{
    public static void main(String args[])
    {
        int target = (int) (Math.random() * 1000); //(int) (Math.random() * 1000)
        System.out.println("The number is " + target);

        Monitor threads = new Monitor();
        threads.start();

        Thread thread0 = new Thread(new Finder(threads, target, 0, 249));
        Thread thread1 = new Thread(new Finder(threads, target, 250, 499));
        Thread thread2 = new Thread(new Finder(threads, target, 500, 749));
        Thread thread3 = new Thread(new Finder(threads, target, 750, 999));

        thread0.start();
        thread1.start();
        thread2.start();
        thread3.start();

        threads.addThread(thread0);
        threads.addThread(thread1);
        threads.addThread(thread2);
        threads.addThread(thread3);
    }

}

Java Code:
import java.util.ArrayList;

public class Finder implements Runnable
{
    private int targetNumber;
    private int startSearch;
    private int endSearch;
    private int count=0;
    private Monitor threadObject;

    public Finder(Monitor thread, int targetNumber, int startSearch, int endSearch)
    {
        this.targetNumber = targetNumber;
        this.startSearch = startSearch;
        this.endSearch = endSearch;

        this.threadObject = thread;
    }
    public void run()
    {
        Thread t = Thread.currentThread();
        for(int a=startSearch; a < endSearch; a++)
        {
            if(targetNumber == a)
            {
                System.out.println("Target number " + targetNumber
                                                    + " found by "+ t.getName());
                threadObject.foundNumber();
            }
            count++;

            if(count == 10)
            {
                try
                {
                    t.sleep(1);
                }
                catch(InterruptedException e)
                {
                    System.out.println(t.getName() + " interrupted");
                }
                count=0;
            }
        }
    }
}

Any help for where I'm going wrong would be much appreciated.