Java – break down work into multiple threads

I have the following problems: for performance reasons, I need to split work across multiple threads, but I don't know what to do

First, the task I will provide should return a value and get a parameter In addition, the main method (doing the main work instead of static main ()) is already running on a separate thread and is called regularly In addition, the method must wait for all threads to complete at a certain point in time before continuing

One approach (most obvious to me) is to schedule each job on a separate thread and store the results in the class vars:

public Object result1,result2;

public void mainMethod() throws InterruptedException {
    final Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            result1 = expensiveMethod("param1");
        }
    });

    final Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            result2 = expensiveMethod("param2");
        }
    });

    thread1.join();
    thread.join();

    //Do rest of work
}

private Object expensiveMethod(Object param){
    // Do work and return result
}

This is a bit ugly and not ideal, because as I said, mainmethod has been called many times, and I don't want any match conditions to set the result variable Ideally, I want to make them local variables, but I can't use them to access them in the run method unless they are final, and then I can't assign values to them

Other methods I'm doing are as follows:

public void mainMethod() throws InterruptedException,ExecutionException {
    String obj1,obj2;

    final ExecutorService executorService = Executors.newFixedThreadPool(16);
    final Future<String> res1 = executorService.submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            return expensiveMethod("param1");
        }
    });
    final Future<String> res2 = executorService.submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            return expensiveMethod("param2");
        }
    });

    obj1 = res1.get();
    obj2 = res2.get();

}

private String expensiveMethod(String param) {
    // Do work and return result
}

This automatically waits for these two calculations from the main method and allows me to store the results locally What do you think? Any other method?

Solution

The way you use executorservice is almost the most modern and secure Simply extract your callable to separate courses:

public class ExpensiveTask implements Callable<String> {

    private final String param;

    public ExpensiveTask(String param) {
        this.param = param;
    }

    @Override
    public String call() throws Exception {
        return expensiveMethod(param);
    }

}

This will make your code cleaner:

final ExecutorService executorService = Executors.newFixedThreadPool(16);
final Future<String> res1 = executorService.submit(new ExpensiveTask("param1");
final Future<String> res2 = executorService.submit(new ExpensiveTask("param2");
String obj1 = res1.get();
String obj2 = res2.get();

Several notes:

>There are too many 16 threads. If you only want to handle two tasks at the same time - or do you want to reuse the pool from multiple client threads? Remember to close the swimming pool > use the lightweight executorcompletionservice to wait for the first task to be completed, not necessarily the first task to be submitted

If you need a completely different design idea, check out akka and its actor based concurrency model)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>