Sobes.tech
Junior — Middle

Have you ever encountered situations where you had to work with parallel task execution in software projects?

sobes.tech AI

Answer from AI

Yes, in Java projects it is common to work with parallel task execution to improve application performance and responsiveness. For example, you can use the ExecutorService class to manage a thread pool, which allows you to run tasks asynchronously and control their execution.

Example of using ExecutorService:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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

        Runnable task1 = () -> System.out.println("Task 1 is running in thread " + Thread.currentThread().getName());
        Runnable task2 = () -> System.out.println("Task 2 is running in thread " + Thread.currentThread().getName());

        executor.submit(task1);
        executor.submit(task2);

        executor.shutdown();
    }
}

This approach helps to efficiently distribute the load and improve application responsiveness.