Creating Threads in Java
In Java, you can create threads using the Thread
class or the Runnable
interface. This tutorial explains both approaches with examples.
Step 1: Creating a Thread by Extending the Thread Class
To create a thread by extending the Thread
class, you need to override its run()
method. Below is an example:
class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Thread running: " + i); try { Thread.sleep(500); // Pause for 500 milliseconds } catch (InterruptedException e) { System.out.println("Thread interrupted: " + e.getMessage()); } } } } public class ThreadExample { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); // Start the thread } }
Step 2: Creating a Thread by Implementing the Runnable Interface
To create a thread by implementing the Runnable
interface, you need to define the run()
method and pass an instance of your class to a Thread
object. Below is an example:
class MyRunnable implements Runnable { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Runnable running: " + i); try { Thread.sleep(500); // Pause for 500 milliseconds } catch (InterruptedException e) { System.out.println("Thread interrupted: " + e.getMessage()); } } } } public class RunnableExample { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start(); // Start the thread } }
Step 3: Using Anonymous Classes for Threads
For simpler use cases, you can create threads using anonymous classes. Below is an example:
public class AnonymousThreadExample { public static void main(String[] args) { Thread thread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Anonymous thread running: " + i); try { Thread.sleep(500); // Pause for 500 milliseconds } catch (InterruptedException e) { System.out.println("Thread interrupted: " + e.getMessage()); } } } }); thread.start(); // Start the thread } }
Step 4: Using Lambda Expressions for Threads (Java 8+)
If you are using Java 8 or later, you can use lambda expressions to simplify thread creation. Below is an example:
public class LambdaThreadExample { public static void main(String[] args) { Thread thread = new Thread(() -> { for (int i = 0; i < 5; i++) { System.out.println("Lambda thread running: " + i); try { Thread.sleep(500); // Pause for 500 milliseconds } catch (InterruptedException e) { System.out.println("Thread interrupted: " + e.getMessage()); } } }); thread.start(); // Start the thread } }
Summary
In this tutorial, you learned:
- How to create a thread by extending the
Thread
class - How to create a thread by implementing the
Runnable
interface - How to use anonymous classes and lambda expressions for thread creation
Threads are a fundamental part of concurrent programming in Java and can be created using multiple approaches to suit different use cases.