Sobes.tech
Junior — Middle

Have you worked with parallelism and multithreading in your projects?

sobes.tech AI

Answer from AI

Yes, in Java projects I have used multithreading to improve performance and implement parallel computations. I worked with classes from the java.util.concurrent package, such as ExecutorService for managing thread pools, and Future for obtaining results of asynchronous tasks.

Example of creating and running multiple threads using ExecutorService:

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

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

        Runnable task = () -> {
            System.out.println("Task is running in thread " + Thread.currentThread().getName());
        };

        for (int i = 0; i < 5; i++) {
            executor.submit(task);
        }

        executor.shutdown();
    }
}

I also encountered synchronization issues and used the synchronized keyword and Lock classes to prevent data races.

Have you worked with parallelism and multithreading… - sobes.tech