Junior — Middle
What components and multithreading mechanisms have you used in your projects?
sobes.tech AI
Answer from AI
In Java projects, I used several core components and mechanisms for multithreading:
- Thread class for creating and starting threads.
- Runnable interface for defining tasks executed in threads.
- ExecutorService and thread pools to manage the number of concurrently running threads and simplify their reuse.
- Synchronization using the
synchronizedkeyword to protect shared resources from concurrent access. - Classes from java.util.concurrent, such as
CountDownLatch,Semaphore,ConcurrentHashMapfor solving synchronization problems and ensuring thread safety.
Example of using ExecutorService:
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.submit(() -> {
System.out.println("Executing a task in a separate thread");
});
executor.shutdown();