Sobes.tech
Junior — Middle

What methods can be used to implement data stream creation in Java?

sobes.tech AI

Answer from AI

In Java, creating data streams (execution threads) can be implemented in several ways:

  1. Inheriting from the Thread class Create a class that inherits from Thread and override the run() method.
class MyThread extends Thread {
    public void run() {
        System.out.println("Thread started");
    }
}

MyThread t = new MyThread();
t.start();
  1. Implementing the Runnable interface Create a class that implements Runnable and pass it to the Thread constructor.
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread via Runnable");
    }
}

Thread t = new Thread(new MyRunnable());
t.start();
  1. Using lambda expressions (since Java 8)
Thread t = new Thread(() -> System.out.println("Thread via lambda"));
 t.start();
  1. Using ExecutorService and thread pools
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> System.out.println("Task in thread pool"));
executor.shutdown();

These methods allow creating and managing threads for parallel task execution.

What methods can be used to implement data stream… - sobes.tech