多线程

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author shiqil.liu
 * @date 2019-08-21 14:26
 */
public class Lotxc {
    static Integer w = 0;
    static AtomicInteger ww = new AtomicInteger(0);
    static CountDownLatch countDownLatch = new CountDownLatch(200000);

    public static void main(String[] args) {
        List<Mytask> list = Lists.newArrayList();
        for (int i = 0; i < 200000; i++) {
            list.add(new Mytask(w));
        }


        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 4, 3000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());
        try {
            List<Future<Integer>> futures = threadPoolExecutor.invokeAll(list);
            for (Future<Integer> f : futures) {
                //System.out.println(f.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            countDownLatch.await();
            System.out.println("最终的w是" + Lotxc.w);
            System.out.println("最终的w是" + Lotxc.ww);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    /*class Mytask implements Callable<String> {
        String temp;

        Mytask(String s) {
            temp = s;
        }

        @Override
        public String call() throws Exception {
            System.out.println(temp);
            return temp+"1";
        }
    }*/
}

class Mytask implements Callable<Integer> {
    Integer temp;

    Mytask(Integer s) {
        temp = s;
    }

    static Object object = new Object();

    @Override
    public Integer call() throws Exception {
        Lotxc.ww.incrementAndGet();
        //synchronized (object) {
        Lotxc.w++;
        //}
        Lotxc.countDownLatch.countDown();
        return Lotxc.ww.incrementAndGet();
    }
}

 

 

import com.google.common.collect.Lists;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

//阿里巴巴不让这么创建,还是用上一种吧
public class Test {
    private static Logger logger = LoggerFactory.getLogger(Test.class);
    static AtomicInteger atomicInteger = new AtomicInteger(0);
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4,r -> new Thread(r, "thread-" + atomicInteger.getAndIncrement()));
        ArrayList< Future<?>> list = Lists.newArrayList();
        for (int i = 0; i < 100; i++) {
            Future<?> submit = executorService.submit(() -> {
                logger.info("abc");
                return 13;

            });
            list.add(submit);
        }


        System.out.println("list的大小为:"+list.size());
        list.forEach(future -> {
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }




}


 

 

posted @ 2019-08-21 19:32  TheQi  阅读(158)  评论(0编辑  收藏  举报