• 并发包---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;
        }
    }
  • 相关阅读:
    关于c#中的委托和事件
    Unity3d中默认函数调用顺序(MonoBehaviour)
    u3d 摄像机详解
    u3d中的坐标系
    u3d中的向量 vector3 vector2
    u3d中的INput
    C#构造函数
    解析C#中[],List,Array,ArrayList的区别及应用
    Mybatis(七) mybatis的逆向工程的配置详解
    Mybatis(六) Spring整合mybatis
  • 原文地址:https://www.cnblogs.com/wqff-biubiu/p/9281872.html
Copyright © 2020-2023  润新知