WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Concurrency · Guru · question 94 of 100

How can you use the Java Fork/Join Framework to implement divide-and-conquer algorithms?

📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

The Java Fork/Join framework is a powerful tool for implementing divide-and-conquer algorithms in Java. It provides a simple and efficient way to parallelize computations across multiple processors, and is particularly well-suited for recursive algorithms with a large amount of independent subtasks.

To use the Fork/Join framework, you need to define a class that extends the RecursiveTask or RecursiveAction class, depending on whether your algorithm returns a value or not. The RecursiveTask class represents a task that returns a result, while the RecursiveAction class represents a task that does not return a result.

Here is an example of a simple RecursiveTask implementation using the Fork/Join framework:

import java.util.concurrent.RecursiveTask;

public class FibonacciTask extends RecursiveTask<Long> {
    private final long n;

    public FibonacciTask(long n) {
        this.n = n;
    }

    @Override
    protected Long compute() {
        if (n <= 1) {
            return n;
        }

        FibonacciTask left = new FibonacciTask(n - 1);
        left.fork();

        FibonacciTask right = new FibonacciTask(n - 2);
        right.fork();

        return left.join() + right.join();
    }
}

In this example, we define a FibonacciTask class that extends the RecursiveTask<Long> class. The n field represents the input value to the Fibonacci function. The compute() method is the main entry point to the algorithm and performs the following steps:

If the input value n is less than or equal to 1, return n.
Create two new FibonacciTask instances for n-1 and n-2.
Fork both of these subtasks to run in parallel.
Wait for both subtasks to complete and return their results.
Combine the results of the subtasks and return the final result.

To execute this algorithm using the Fork/Join framework, we need to create an instance of the FibonacciTask class and submit it to a ForkJoinPool. Here is an example:

import java.util.concurrent.ForkJoinPool;

public class Main {
    public static void main(String[] args) {
        FibonacciTask task = new FibonacciTask(10);
        ForkJoinPool pool = new ForkJoinPool();
        Long result = pool.invoke(task);
        System.out.println(result);
    }
}

In this example, we create a new FibonacciTask instance for the input value 10, and submit it to a new ForkJoinPool using the invoke() method. This method blocks until the computation is complete and returns the final result.

Overall, the Fork/Join framework provides a powerful and efficient way to parallelize divide-and-conquer algorithms in Java, and can lead to significant performance improvements on multi-core processors.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview — then scores it.
📞 Practice Java Concurrency — free 15 min
📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

All 100 Java Concurrency questions · All topics