使用生产者消费者模式和阻塞队列进行0到一千万的累加

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class BigNumberAnd {
		
	//有界阻塞队列
    static ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue(100);

    public static void main(String[] args) throws InterruptedException {

		//计算数大小
        long count = 10000000l;

		//生产者开始生产的最小值
        AtomicInteger produceBegin = new AtomicInteger(0);
        //消费者开始消费的最小值
        AtomicInteger comsumerBegin = new AtomicInteger(0);
        //记录结果的大小
        AtomicLong result = new AtomicLong(0);

        for (int i = 0; i < 2; i++) {
            new Thread(new Produce(produceBegin, queue,count)).start();
        }

        ExecutorService executorService = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 50; i++) {
            executorService.submit(new Comsumer(queue, comsumerBegin, result,count));
        }

        executorService.shutdown();

        while (executorService.isTerminated()) {
            System.out.println(result.get());
        }
    }
}


class Comsumer implements Runnable {

    ArrayBlockingQueue<Integer> queue;
    AtomicInteger begin;
    AtomicLong sum;
    Long Count;

    public Comsumer(ArrayBlockingQueue<Integer> queue, AtomicInteger begin, AtomicLong sum,Long count) {
        this.queue = queue;
        this.sum = sum;
        this.begin = begin;
        this.Count = count;
    }

    @Override
    public void run() {
        do {
            try {
                Integer element = queue.poll(1000, TimeUnit.MILLISECONDS);
                if (element == null) {
                    if (queue.isEmpty() && begin.get() >= Count) {
                        System.out.println("-----------消费者" + Thread.currentThread().getName() + "退出,结果为:" + sum);
                        return;
                    }
                } else {
                    sum.addAndGet(element);
                    begin.incrementAndGet();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (true);
    }


}


class Produce implements Runnable {

    AtomicInteger digital;
    ArrayBlockingQueue<Integer> queue;
    Long Count;

    public Produce(AtomicInteger digital, ArrayBlockingQueue<Integer> queue,Long count) {
        this.digital = digital;
        this.queue = queue;
        this.Count = count;
    }

    @Override
    public void run() {
        do {
            int taskValue = digital.incrementAndGet();
            if (taskValue > Count) {
                System.out.println("-------------生产者线程" + Thread.currentThread().getName() + "退出,最终值:" + taskValue);
                return;
            } else {
                try {
                    queue.put(taskValue);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } while (true);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值