package primes;

public class NumberProcessor {

    public boolean isNumberPrime(long number) {

        long end = number / 2;

        for (long i = 2; i <= end; i++) {

            if (number % i == 0) {

                return false;

            }

        }

        return true;

    }

}

 

 

 

 

package primes;

public class NumberProcessor implements Runnable {

    private long numToProcess;

     

    public NumberProcessor(long numToProcess) {

        this.numToProcess = numToProcess;

    }

    public boolean isNumberPrime(long number) {

        long end = number / 2;

        for (long i = 2; i <= end; i++) {

            if (number % i == 0) {

                return false;

            }

        }

        return true;

    }

    @Override

    public void run() {

        boolean result = isNumberPrime(numToProcess);

        if (result == true) {

            System.out.println(numToProcess + " is a prime number");

        } else {

            System.out.println(numToProcess +

              " is not a prime number");

        }  

    }

}

 

 

 

 

package primes;

public class Main {

    public static void main(String[] args) {

        for (int i = 0; i < args.length; i++) {

            long number = Long.valueOf(args[i]);

            NumberProcessor numberProcesssor = new

                NumberProcessor(number);

            Thread t = new Thread(numberProcesssor);

            t.start();

        }

    }

}

 

 

 

 

@Override

public void run() {

    try {

        Thread.sleep((long) Math.log(numToProcess));

    } catch (InterruptedException e) {}

    System.out.println("Beginning to process " + numToProcess);

    boolean result = isNumberPrime(numToProcess);

    if (result == true) {

        System.out.println(numToProcess + " is a prime number");

    } else {

        System.out.println(numToProcess + " is not a prime number");

    }

}

 

 

 

 

package primes;

public class ThreadTracker {

    private int threadsRunning = 0;

    public void setThreadRunning() {

        threadsRunning++;

        System.out.println(threadsRunning + " threads running");

    }

    public void setThreadFinished() {

        threadsRunning--;

        System.out.println(threadsRunning + " threads running");

    }

}

 

 

 

 

package primes;

public class NumberProcessor implements Runnable {

    private long numToProcess;

    private ThreadTracker tracker;

    public NumberProcessor(long numToProcess,

           ThreadTracker tracker) {

        this.numToProcess = numToProcess;

        this.tracker = tracker;

    }

    public boolean isNumberPrime(long number) {

        long end = number / 2;

        for (long i = 2; i <= end; i++) {

            if (number % i == 0) {

                return false;

            }

        }

        return true;

    }

    @Override

    public void run() {

        try {

            Thread.sleep((long) Math.log(numToProcess));

        } catch (InterruptedException e) {}

        tracker.setThreadRunning();

        boolean result = isNumberPrime(numToProcess);

        if (result == true) {

            System.out.println(numToProcess + " is a prime number");

        } else {

             System.out.println(numToProcess

                + " is not a prime number");

        }

        tracker.setThreadFinished();

    }

}

 

 

 

 

package primes;

public class Main {

    public static void main(String[] args) {

        ThreadTracker tracker = new ThreadTracker();

        for (int i = 0; i < args.length; i++) {

            long number = Long.valueOf(args[i]);

            NumberProcessor numberProcesssor = new

                NumberProcessor(number, tracker);

            Thread t = new Thread(numberProcesssor);

            t.start();

        }

    }

}

 

 

 

 

package threads;

public class WaitExample implements Runnable {

    private int resultsObtained = 0;

 

    public static void  main(String[] args) {

        WaitExample we = new WaitExample();

        for (int i = 0; i < 10; i++) {

            Thread t = new Thread(we);

            t.start();

        }

        we.printResults();

    }

     

    public synchronized void printResults() {

        while(resultsObtained < 5) {

            try {

                System.out.println("Waiting for enough results");

                 wait();           

             } catch (InterruptedException e) {}

        }

        System.out.println("We have received enough results");

    }

     

    public synchronized void addResult() {

        resultsObtained++;

        System.out.println("A result has been added");

        notifyAll();

    }

    @Override

    public void run() {

    try {

        Thread.sleep((long) (Math.random()*10000));              

    } catch (InterruptedException e) {}

        addResult();

    }

}