Creating a Thread Part Two Tutorial

This tutorial will directly build on concepts and source code from my Creating a Thread Part One Tutorial. In that tutorial I demonstrated how to create and start a new thread from a class that implements the Runnable interface. In this tutorial I will show you how to create and start a new thread by creating a class that extends the Thread class. The prime-number-calculating content will be almost identical to part one.



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 CreatingAThreadTwo
C:\Java>cd CreatingAThreadTwo
C:\Java\CreatingAThreadTwo>Notepad CreatingAThreadTwo.java

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


class CreatingAThreadTwo {
    public static void main(String args[]) {
        long myNum = 9223372036854775783L;

        System.out.println("Number to factor: " + myNum);
    
        MyCalculations mc = new MyCalculations(myNum, "myChildThread");

        Thread mainThread = Thread.currentThread(); // main thread
        int spinner = 0;
        char animation = ' ';
        System.out.println("Active threads for this thread group: " + Thread.activeCount());
        System.out.print("Calculating  ");
        while(mc.isAlive()) {
            spinner++;
            switch(spinner) {
                case 1: 
                    animation = '|';
                    break;
                case 2: 
                    animation = '/';
                    break;
                case 3:
                    animation = '-';
                    break;
                case 4:
                    animation = '\\';
                    spinner = 0;
                    break;
            }
            System.out.print("\b"+animation);

            try {
                mainThread.sleep(200);
            } catch (InterruptedException e) {
                System.out.println("Main thread interrupted");
            }
        }
        System.out.println("Active threads for this thread group: " + Thread.activeCount());
    }
}

class MyCalculations extends Thread {
    private long numberToFactor = 0;

    MyCalculations(long numberToFactor, String threadName){
        super(threadName);
        this.numberToFactor = numberToFactor;
        start();
    }

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

    private void isItPrime() { // only designed to work with odd numbers
        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("\n" + numberToFactor + " is NOT Prime ... first divisible by " + i);
                return;
            }
        }
        System.out.println("\n" + numberToFactor + " is Prime!");
        return;
    }
}

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


C:\Java\CreatingAThreadTwo>javac CreatingAThreadTwo.java
C:\Java\CreatingAThreadTwo>java CreatingAThreadTwo
Number to factor: 9223372036854775783
Active threads for this thread group: 2
Calculating \
9223372036854775783 is Prime!
Active threads for this thread group: 1


Final thoughts

Both ways of creating threads accomplish the same result, they only differ in whether you extends Thread or implements Runnable. My personal preference is to go with version that implements Runnable option, but it really doesn't make a difference one way or another.


Tutorials