Java - splitting work to multiple threads

前端 未结 6 1979
谎友^
谎友^ 2020-12-14 12:07

I am posed with the following problem: I need to split work across multiple threads for perfomance reasons, but I am not sure what approach to take.

Firstly, the tas

6条回答
  •  一个人的身影
    2020-12-14 12:22

    I shall add a proposal that is in my eyes more elegant than creating a whole new class for your parametrized Callable. My solution is a method that returns a Callable instance:

    Callable expensive(final String param) {
      return new Callable() { public String call() { 
        return expensiveMethod(param);
      }};
    }
    

    This even makes client code more palatable:

    final Future f1 = executor.submit(expensive("param1"));
    

提交回复
热议问题