Javac · Java Tutorials - Creating a Thread Part Four Tutorial

Creating a Thread Part Four Tutorial

This tutorial will directly build on concepts and source code from my Creating a Thread Part Three Tutorial. In this tutorial I will demonstrate how to make a thread object automatically start itself up when the object is created. In other words, I'll have the constructor create a new Thread object passing itself as a Runnable parameter and then invoking the start() method.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md CreatingAThreadFour
C:\Java>cd CreatingAThreadFour
C:\Java\CreatingAThreadFour>Notepad CreatingAThreadFour.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are dFour.


import java.util.*;
class CreatingAThreadFour {

    public static void main(String args[]) {

        Thread mainThread = Thread.currentThread(); // main thread
        System.out.println("Calculating primes: ");
        for (long x = 6223372036854775799L; x <= 6223372036854775999L; x+=2) {
            new MyCalculations(x);
        }
        System.out.println("Total number of active threads: " + Thread.activeCount());
        int spinner = 0;
        String animation = "Calculating ";
        while(Thread.activeCount() > 1) {
            spinner++;
            switch(spinner) {
                case 1: 
                animation = "Calculating ";
                break;
            case 2: 
                animation = "            ";
                spinner = 0;
                break;
            }
            System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b"+animation);

            try {
                mainThread.sleep(300);
            } catch (InterruptedException e) {
                System.out.println("Main thread interrupted");
            }
        }
    }

}

class MyCalculations implements Runnable {
    private long numberToFactor = 0;

    MyCalculations(long numberToFactor){
        this.numberToFactor = numberToFactor;
        new Thread(this).start();
    }

    @Override
    public void run() { // this is where the new thread starts
        isItPrime(numberToFactor);
    }

    void isItPrime(long numberToFactor) { 
        if (numberToFactor % 2 == 0) {
            System.out.println(numberToFactor + " is NOT Prime ... divisible by 2");
            return;
        }
        long squareRoot = (long) Math.sqrt(numberToFactor);
        for (long i = 3; i <= squareRoot; i+=2) {
            if (numberToFactor % i == 0) { // when the remainder is 0 the number is evenly divisible - hence not prime
                System.out.println(numberToFactor + " is NOT Prime ... first divisible by " + i);
                return;
            }
        }
        System.out.println(numberToFactor + " is Prime!!!");
    }
}


Now switch back to the command prompt (CMD) and type in javac CreatingAThreadFour.java and press Enter.
Now type in java CreatingAThreadFour and press Enter.


C:\Java\CreatingAThreadFour>javac CreatingAThreadFour.java
C:\Java\CreatingAThreadFour>java CreatingAThreadFour
see video for results


Final thoughts


Tutorials