Java Threads

Threads help to perform multiple things at the same time. For example, listening to the radio while eating.

Create a Java thread

There are two ways to create a Java thread: extends the Thread class or implements the Runnable interface.

Extend the Thread class

Extend the Thread class and override its run() method. Code example:

MyThread1 class:

package com.devspeedup.java.thread.thread;

public class MyThread1 extends Thread {
	
	public void run() {
		for (int i = 1; i < 100; i++) {
			System.out.println("Thread 1: eating - " + i);
		}
	}
}

MyThread2 class:

package com.devspeedup.java.thread.thread;

public class MyThread1 extends Thread {
	
	public void run() {
		for (int i = 1; i < 100; i++) {
			System.out.println("Thread 1: eating - " + i);
		}
	}
}

TestThread class:

package com.devspeedup.java.thread.thread;

public class TestThread {

	public static void main(String[] args) {
		Thread t1 = new MyThread1();
		t1.start();
		Thread t2 = new MyThread2();
		t2.start();
	}
}

Test result:

Implement the Runnable interface

Implement the Runnable interface and implement its run() method. Code example:

MyThread1 class:

package com.devspeedup.java.thread.runnable;

public class MyThread1 implements Runnable {
	
	public void run() {
		for (int i = 1; i < 100; i++) {
			System.out.println("Thread 1: eating - " + i);
		}
	}
}

MyThread2 class:

package com.devspeedup.java.thread.runnable;

public class MyThread2 implements Runnable {
	
	public void run() {
		for (int i = 1; i < 100; i++) {
			System.out.println("Thread 2: listening to the radio - " + i);
		}
	}
}

TestThread class:

package com.devspeedup.java.thread.runnable;

public class TestThread {

	public static void main(String[] args) {
		
		Runnable obj1 = new MyThread1();
		Runnable obj2 = new MyThread2();
		
		Thread t1 = new Thread(obj1);
		Thread t2 = new Thread(obj2);
		
		t1.start();
		t2.start();
	}
}

Test result:

Calling start() vs run()

Calling the start() method creates a new thread while calling the run() method doesn’t. If you mean to create a new thread but calling the run() method, it is a mistake.

Try both examples above again but this time instead of calling the start() method, try to call the run() method directly and check the results.

TestThread2 classes:

package com.devspeedup.java.thread.thread;

public class TestThread2 {

	public static void main(String[] args) {
		
		Thread t1 = new MyThread1();
		Thread t2 = new MyThread2();
		
		// Mistakenly calling the run() method
		t1.run();
		t2.run();
	}
}
package com.devspeedup.java.thread.runnable;

public class TestThread2 {

	public static void main(String[] args) {
		
		Runnable obj1 = new MyThread1();
		Runnable obj2 = new MyThread2();
		
		Thread t1 = new Thread(obj1);
		Thread t2 = new Thread(obj2);
		
		// Mistakenly calling the run() method
		t1.run();
		t2.run();
	}
}

Calling the run() method resulted in the run() method of t1 finishes first then the run() method in t2 starts. This is not what we want to achieve: multithreading (having multiple threads running concurrently).