Creating and running a thread in Java

In this example, we will create and running a thread in a java application. This example is very basic but it helps to understand how a thread can be created and executed.

We can create a thread in two ways.

  1. Implementing Runnable interface: You can create a thread by passing a class that implements the Runnable interface.
  2. Extending Thread class: By extending the Thread class and override the run() method.

Implementing the Runnable interface

We chose to implement the Runnable interface which implicitly implies that we need to implement the run() method. This method will be invoked by the thread that is created.

package com.memorynotfound.thread;

public class Calculator implements Runnable {

    private int number;

    public Calculator(int number) {
        this.number=number;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 6; i++){
            System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), number, i, i*number);
        }
    }
}

Fire a couple of threads

package com.memorynotfound.thread;

public class RunnableProgram {

    public static void main(String... args){
        for (int i=1; i<=10; i++){
            Calculator calculator = new Calculator(i);
            Thread thread = new Thread(calculator);
            thread.start();
            System.out.println("Thread with name: " + thread.getName() + " started");
        }
    }
}

Result

Thread with name: Thread-0 started
Thread-0: 1 * 1 = 1
Thread-0: 1 * 2 = 2
Thread-0: 1 * 3 = 3
Thread-0: 1 * 4 = 4
Thread-0: 1 * 5 = 5
Thread-0: 1 * 6 = 6
Thread-1: 2 * 1 = 2
Thread-1: 2 * 2 = 4
Thread-1: 2 * 3 = 6
Thread-1: 2 * 4 = 8
Thread-1: 2 * 5 = 10
Thread-1: 2 * 6 = 12
Thread with name: Thread-1 started
Thread with name: Thread-2 started
Thread with name: Thread-3 started
Thread-2: 3 * 1 = 3
Thread-2: 3 * 2 = 6
Thread-2: 3 * 3 = 9
Thread-2: 3 * 4 = 12
Thread-2: 3 * 5 = 15
Thread-2: 3 * 6 = 18
Thread-4: 5 * 1 = 5
Thread-4: 5 * 2 = 10
Thread-4: 5 * 3 = 15
Thread-4: 5 * 4 = 20
Thread-4: 5 * 5 = 25
Thread-4: 5 * 6 = 30
Thread-3: 4 * 1 = 4
Thread-3: 4 * 2 = 8
Thread-3: 4 * 3 = 12
Thread-3: 4 * 4 = 16
Thread-3: 4 * 5 = 20
Thread-3: 4 * 6 = 24
Thread with name: Thread-4 started
Thread with name: Thread-5 started
Thread-5: 6 * 1 = 6
Thread-5: 6 * 2 = 12
Thread-5: 6 * 3 = 18
Thread-5: 6 * 4 = 24
Thread-5: 6 * 5 = 30
Thread-5: 6 * 6 = 36
Thread-6: 7 * 1 = 7
Thread-6: 7 * 2 = 14
Thread-6: 7 * 3 = 21
Thread-6: 7 * 4 = 28
Thread-6: 7 * 5 = 35
Thread-6: 7 * 6 = 42
Thread with name: Thread-6 started
Thread with name: Thread-7 started
Thread with name: Thread-8 started
Thread-7: 8 * 1 = 8
Thread-7: 8 * 2 = 16
Thread-7: 8 * 3 = 24
Thread-7: 8 * 4 = 32
Thread-7: 8 * 5 = 40
Thread-7: 8 * 6 = 48
Thread-9: 10 * 1 = 10
Thread-9: 10 * 2 = 20
Thread-9: 10 * 3 = 30
Thread-9: 10 * 4 = 40
Thread-9: 10 * 5 = 50
Thread-9: 10 * 6 = 60
Thread with name: Thread-9 started
Thread-8: 9 * 1 = 9
Thread-8: 9 * 2 = 18
Thread-8: 9 * 3 = 27
Thread-8: 9 * 4 = 36
Thread-8: 9 * 5 = 45
Thread-8: 9 * 6 = 54

References

You may also like...