• 并发包---Future模式


    import java.util.concurrent.Callable;
    import java.util.concurrent.FutureTask;
    
    public class FutureTest {
        public static void main(String s[]){
            FutureTask<Integer> future1 = new FutureTask<Integer>(new Test1());
            FutureTask<String> future2 = new FutureTask<String>(new Test2());
            FutureTask<Long> future3 = new FutureTask<Long>(new Test3());
            new Thread(future1).start();
            new Thread(future2).start();
            new Thread(future3).start();
            try {
                if (future1.isDone()){
                    System.out.println("future1 is Done");
                }
                if (future2.isDone()){
                    System.out.println("future2 is Done");
                }
                if (future3.isDone()){
                    System.out.println("future3 is Done");
                }
                int i = future1.get();
                String str = future2.get();
                long l = future3.get();
                System.out.println(i+str+l);
            }catch (Exception e){
                e.printStackTrace();
            }
    
        }
    }
    
    class Test1 implements Callable<Integer>{
        @Override
        public Integer call() {
            try {
                Thread.sleep(1000);
            }catch (Exception e){
            }
            return 1;
        }
    }
    
    class Test2 implements Callable<String>{
        @Override
        public String call() {
            try {
                Thread.sleep(5000);
            }catch (Exception e){
            }
            return "2";
        }
    }
    
    class Test3 implements Callable<Long>{
        @Override
        public Long call() {
            try {
                Thread.sleep(10000);
            }catch (Exception e){
            }
            return 3L;
        }
    }
  • 相关阅读:
    Python-Basis-9th
    Python-Basis-8th
    Python-Basis-7th
    Ubuntu-Basis-4th
    Ubuntu-Basis-3rd
    Ubuntu-Basis-2nd
    Ubuntu-Basis-1st
    疯狂java第五章&&第六章-面向对象
    疯狂java第四章-流程控制与数组
    疯狂java第三章-数据类型和运算符
  • 原文地址:https://www.cnblogs.com/wqff-biubiu/p/9281872.html
Copyright © 2020-2023  润新知