How to use java Future

package com.example.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/* *
* This article describes Callable and Future, which are very interesting, one produces the result, and the other gets the result.
* Created by weng.junjie on 2017/2/16.
*/
public class FutureTest {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> list=new ArrayList<>();
        for (int i= 0;i<10;i++){
            list.add(i);
        }
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future future=executor.submit(()->{
            while(list.size()>0){
                System.out.println(list.remove(0));
            }
        });
        //Simulate to monitor whether multi-threading completes the task ?
        System.out.println(future.isDone());
        Thread.sleep(1000);
        System.out.println(future.isDone());
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326940782&siteId=291194637