User Tools

Site Tools


java:threads:hello-world

Java Threads Hello World Example

Extending Thread

HelloWorldThread.java
// Import necessary classes
import java.lang.Thread;
 
// HelloWorldThread class extending the Thread class
public class HelloWorldThread extends Thread {
 
    @Override
    public void run() {
        // This code will be executed in a separate thread
        System.out.println("Hello, World from a thread!");
    }
 
    public static void main(String[] args) {
        // Create a new instance of the HelloWorldThread
        HelloWorldThread myThread = new HelloWorldThread();
 
        // Start the thread using the start() method
        myThread.start();
    }
}

The HelloWorldThread Class: This class extends Java's Thread class. By extending Thread, HelloWorldThread becomes a thread itself.

The run Method: This is the method that the thread will execute when it is started. In our example, the run method simply prints a message to the console.

Creating and Starting the Thread: In the main method, an instance of HelloWorldThread is created, and the thread is started using the start method. The start method will call the run method on a new thread.

This example demonstrates the basic structure and execution of a thread in Java, showing how easily a simple task can be performed concurrently using threads.

Implementing Runnable

HelloWorldRunnable.java
// Import necessary classes
import java.lang.Runnable;
import java.lang.Thread;
 
// HelloWorldRunnable class implementing the Runnable interface
public class HelloWorldRunnable implements Runnable {
 
    @Override
    public void run() {
        // Code executed when the thread runs
        System.out.println("Hello, World from a Runnable!");
    }
 
    public static void main(String[] args) {
        // Create a new instance of a Thread with HelloWorldRunnable
        Thread myThread = new Thread(new HelloWorldRunnable());
 
        // Start the thread
        myThread.start();
    }
}

Implementing Runnable: When a class implements the Runnable interface, it needs to override the run method. This method will be executed when the thread is started. To run the Runnable, you create an instance of Thread, providing the Runnable as an argument.

Implementing Callable

HelloWorldCallable.java
// Import necessary classes
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ExecutionException;
 
// HelloWorldCallable class implementing the Callable interface
public class HelloWorldCallable implements Callable<String> {
 
    @Override
    public String call() throws Exception {
        // Return a value when the thread completes
        return "Hello, World from a Callable!";
    }
 
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // Create a FutureTask that wraps the HelloWorldCallable
        FutureTask<String> futureTask = new FutureTask<>(new HelloWorldCallable());
 
        // Create a new Thread with the FutureTask
        Thread myThread = new Thread(futureTask);
 
        // Start the thread
        myThread.start();
 
        // Get the result of the computation
        String result = futureTask.get();
        System.out.println(result);
    }
}

Implementing Callable: Callable is similar to Runnable, but it can return a value and throw exceptions. A class implementing Callable must override the call method. To execute a Callable, you use a FutureTask. The FutureTask can be passed to a Thread to be executed, and the result can be obtained through the get method of the FutureTask.

Runnable with lambda expression

LambdaRunnable.java
public class LambdaRunnable {
 
    public static void main(String[] args) {
        // Creating and starting a thread using a lambda expression
        new Thread(() -> System.out.println("Hello, World from a thread with lambda!")).start();
    }
}

Lambda Expression: The lambda expression () → System.out.println(“Hello, World from a thread with lambda!”) is a concise way to implement the Runnable interface. It replaces the need to explicitly define a Runnable class or override the run method.

Thread Creation and Execution: The Thread constructor takes the lambda expression as its argument, creating a thread that executes the code specified in the lambda when started.

This approach is useful for quick and simple thread implementations, where the entire thread logic is concise enough to fit into a single lambda expression.

Callable with lambda expression

LambdaCallable.java
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ExecutionException;
 
public class LambdaCallable {
 
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // Creating a Callable using a lambda expression
        Callable<String> callable = () -> "Hello, World from a Callable with lambda!";
 
        // Wrapping the Callable in a FutureTask and starting a thread
        FutureTask<String> futureTask = new FutureTask<>(callable);
        new Thread(futureTask).start();
 
        // Printing the result
        System.out.println(futureTask.get());
    }
}

Lambda for Callable: The lambda expression () → “Hello, World from a Callable with lambda!” provides a simple implementation of the Callable interface. It returns a string when called.

Using FutureTask: FutureTask is used to wrap the Callable object. The FutureTask is then passed to a new Thread object and started. This allows the Callable to be executed in a separate thread.

Getting the Result: The get method of FutureTask is used to retrieve the result of the Callable. This call blocks until the computation is complete. This approach is ideal for cases where the task to be executed in a thread returns a result or might throw an exception.

Using lambda expressions for Runnable and Callable provides a more streamlined and modern approach to working with threads in Java, especially for simpler tasks where defining separate classes or anonymous inner classes would be overkill.

java/threads/hello-world.txt · Last modified: 2024/01/17 02:11 by odefta