Sobes.tech
Junior — Middle

How did you implement parallel processes in your projects?

sobes.tech AI

Answer from AI

To implement parallel processes in Java, I used several approaches depending on the task:

  • Threads: creating and managing threads manually through the Thread class or Runnable interface.
  • ExecutorService: a more convenient way to manage thread pools and asynchronous task execution.
  • ForkJoinPool: for tasks that can be broken down into subtasks (recursive parallel execution).
  • Parallel Streams: for processing collections in multiple threads.

Example using ExecutorService:

import java.util.concurrent.*;

public class ParallelExample {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newFixedThreadPool(3);

        Callable<String> task = () -> {
            Thread.sleep(1000);
            return "Task result";
        };

        Future<String> future = executor.submit(task);
        System.out.println(future.get());

        executor.shutdown();
    }
}
How did you implement parallel processes in your… - sobes.tech