Creating and Running thread in Java

Like any other Object in java, Threads are Objects. There are a couple of ways threads can be created in Java.

  1. Extending the Thread class and overriding it’s Run() method
  2. Building a class that implements Runnable Interface and implementing it’s run() method. Then create an Object of the Thread class and pass an instance of Runnable object as parameter.

The second approach is a recommended approach but you may use first approach in simple applications. We will be using second approach for our example below for a couple of reasons: first- implementing an interface gives us an opportunity to extend other class if required. second- Extending a class is basically done to enhance or modify it’s functionality, which is not what we want to do. we just want a thread to run our stuff.

In our example we will create 5 threads each printing multiplication table of a number between 1 and 5.

package com.bharatonjava.examples.threads;

public class Calculator implements Runnable {

	private int number;

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

	@Override
	public void run() {

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

	public static void main(String[] args) {

		for (int i = 1; i <= 5; i++) {
			Calculator calculator = new Calculator(i);
			Thread thread = new Thread(calculator);
			thread.start();
		}
	}

}

Output

Threads are unpredictable in the way they execute, so output sequence will be different each time you execute the example. On my machine the output was as below:

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-1: 2 * 7 = 14
Thread-1: 2 * 8 = 16
Thread-1: 2 * 9 = 18
Thread-1: 2 * 10 = 20
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-3: 4 * 7 = 28
Thread-3: 4 * 8 = 32
Thread-3: 4 * 9 = 36
Thread-3: 4 * 10 = 40
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-0: 1 * 7 = 7
Thread-0: 1 * 8 = 8
Thread-0: 1 * 9 = 9
Thread-0: 1 * 10 = 10
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-2: 3 * 7 = 21
Thread-2: 3 * 8 = 24
Thread-2: 3 * 9 = 27
Thread-2: 3 * 10 = 30
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-4: 5 * 7 = 35
Thread-4: 5 * 8 = 40
Thread-4: 5 * 9 = 45
Thread-4: 5 * 10 = 50

References

http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Advertisements

, , ,

  1. Returning Data from Java Threads | Bharat on Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: