Follow these steps to implement the example:
- Create a class named Calculator that implements the Runnable interface:
public class Calculator implements Runnable {
- Implement the run() method. This method will execute the instructions of the thread we are creating, so this method will calculate the prime numbers within the first 20000 numbers:
@Override public void run() { long current = 1L; long max = 20000L; long numPrimes = 0L; System.out.printf("Thread '%s': START\n", Thread.currentThread().getName()); while (current <= max) { if (isPrime(current)) { numPrimes++; } current++; } System.out.printf("Thread '%s': END. Number of Primes: %d\n", Thread.currentThread().getName(), numPrimes); }
- Then, implement ...