Java Multithreading вопросы и ответы для интервью
Вопрос 26. What is the purpose of the Phaser class in Java?
The Phaser class in Java provides a more flexible alternative to the CountDownLatch and CyclicBarrier for synchronizing threads. It supports dynamic registration of threads and phase advancement.
Example:
Phaser phaser = new Phaser();
phaser.register(); // Register the current thread
Вопрос 27. Explain the concept of the ReadWriteLock interface in Java.
The ReadWriteLock interface provides a lock that allows multiple threads to read a resource simultaneously but only one thread to write to the resource at any given time.
Example:
ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
Вопрос 28. What is the purpose of the Exchanger class in Java?
The Exchanger class provides a synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object on entry to the exchange method, matches with a partner thread, and receives its partner's object on return.
Example:
Exchangerexchanger = new Exchanger<>();
Вопрос 29. Explain the concept of the ForkJoinPool in Java.
The ForkJoinPool is a special-purpose ExecutorService designed for parallelizing divide-and-conquer algorithms. It is particularly useful for recursive tasks with multiple subtasks.
Example:
RecursiveTask and RecursiveAction are classes commonly used with ForkJoinPool.
Вопрос 30. What is the purpose of the CompletableFuture class in Java?
CompletableFuture is a class in Java that represents a promise to complete an asynchronous computation. It provides a flexible way to compose, combine, and manage asynchronous operations.
Example:
CompletableFuture.supplyAsync(() -> /* Asynchronous computation */ ).thenApply(result -> /* Process result */ );
Самое полезное по оценкам пользователей:
- What is multithreading?
- What is the purpose of the yield() method?
- What is the purpose of the sleep() method in Java?
- How do you create a thread in Java?
- Explain the difference between Thread and Runnable.