• java线程同步--实现Callable接口


    package com.LearnJava.Thread;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    class numThread implements Callable {
    
        @Override
        public Object call () {
            int sum =0;
            for(int i=0;i<100;i++){
                if(i%2==0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sum+=i;
                }
            }
            return sum;
    
        }
    }
    public class ThreadTest3 {
        public static void main(String[] args) {
            numThread nt = new numThread();
    
            FutureTask f = new FutureTask(nt);
    
            new Thread(f){}.start();
    
            try {
                Object sum = f.get();
                System.out.println(sum);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    文件操作
    安全名词
    浏览器并发连接
    acm 2057
    acm 2072
    acm 2084
    acm 2044
    acm 2043
    acm 2032
    acm 2005
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/12284630.html
Copyright © 2020-2023  润新知