素数多线程实现

package com.tool;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by xing on 2019-03-14.
 */
public class SuShuExample implements Callable<Boolean> {

    private AtomicInteger atomicInteger;
    private CountDownLatch countDownLatch;
    private int num;
    public SuShuExample(AtomicInteger atomicInteger, CountDownLatch countDownLatch,int num){
        this.countDownLatch = countDownLatch;
        this.atomicInteger = atomicInteger;
        this.num = num;
    }
    @Override
    public Boolean call() throws Exception {
        countDownLatch.countDown();
        int j=2;
        while(num%j != 0){
            j++;
            if (num == j){
                System.out.println(num);
                atomicInteger.getAndIncrement();
                System.out.println("count : " + atomicInteger.get());
                Thread.sleep(10000);
                return true;
            }
        }
        return false;
    }
}

 

 

package com.tool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by xing on 2019-03-14.
 */
public class RunSuShu {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        AtomicInteger atomicInteger = new AtomicInteger(0);
        CountDownLatch countDownLatch = new CountDownLatch(10);
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        List<Future> futureList = new ArrayList<>();
        for (int i =0;i<10;i++){
            SuShuExample suShuExample = new SuShuExample(atomicInteger,countDownLatch,i);
            futureList.add(executorService.submit(suShuExample));
        }
        //第一种方案;使用Future 获取 返回值
        try {
            int count = 0;
            for (int j =0;j<futureList.size();j++){
                try {
                    Boolean r = (Boolean)futureList.get(j).get();
                    if (r){
                        count ++;
                    }
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("sushu:" + count);
            //方案二:引入countDownLatch\Actomic,应为是多线程要保证原子性
            countDownLatch.await();
            System.out.println("sushu:" + atomicInteger.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            long endTime = System.currentTimeMillis();
            System.out.println("time:" + String.valueOf(endTime - startTime));
            executorService.shutdown();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值