What does 'result' in ExecutorService.submit(Runnable task, T result) do?

前端 未结 3 1904
闹比i
闹比i 2020-12-16 11:49

Looking at the javadocs it just says

Future submit(Runnable task, T result)

Submits a Runnable task for executio

3条回答
  •  佛祖请我去吃肉
    2020-12-16 12:13

    You can mutate the object that was passed-in during the task. For example:

    final String[] mutable = new String[1];
    Runnable r = new Runnable() {
        public void run() {
            mutable[0] = "howdy";
        }
    };
    Future f = executorService.submit(r, mutable);
    String[] result = f.get();
    System.out.println("result[0]: " + result[0]);
    

    When I run this code it outputs:

    result[0]: howdy
    

提交回复
热议问题