Java—多线程实现生产者消费者模型

采用线程实现“生产者-消费者”编程的基础模型

源代码

消费者代码:
public class Consumer implements Runnable {
    BlockingQueue<Integer> blockingQueue;
    int n;
    CountDownLatch countDownLatch;
    public Consumer(BlockingQueue<Integer> blockingQueue, int n, CountDownLatch countDownLatch) {
        this.blockingQueue = blockingQueue;
        this.n = n;
        this.countDownLatch=countDownLatch;
    }
    @Override
    public void run() {
         for(int i=0;i<n;i++)
         {
             try {
                 int cur=blockingQueue.take();
                 isPrime(cur);
              /*   System.out.println("消费"+blockingQueue.size());*/
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
        System.out.println("消费者完成");
         countDownLatch.countDown();
    }


    int isPrime(int n)
    {	//返回1表示判断为质数,0为非质数,在此没有进行输入异常检测
        double n_sqrt;
        if(n==2 || n==3) return 1;
        if(n%6!=1 && n%6!=5) return 0;
        n_sqrt=Math.floor(Math.sqrt((float)n));
        for(int i=5;i<=n_sqrt;i+=6)
        {
            if(n%(i)==0 | n%(i+2)==0) return 0;
        }
        return 1;
    }

}

生产者代码:
public class Producer implements Runnable{
    BlockingQueue<Integer> blockingQueue;
    int n;
    CountDownLatch countDownLatch;
    public Producer(BlockingQueue<Integer> blockingQueue, int n,CountDownLatch countDownLatch) {
        this.blockingQueue = blockingQueue;
        this.n = n;
        this.countDownLatch=countDownLatch;
    }

    @Override
    public void run() {
        Random ra =new Random();
        for(int i=0;i<n;i++)
        {
            try {
                blockingQueue.put(ra.nextInt(2000000000)+1);
            /*    System.out.println("生产"+blockingQueue.size());*/
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("生产者完成");
        countDownLatch.countDown();
    }
}

主类:
public class Model {
    public  static void excute(int producerNum,int consumerNum,int num,CountDownLatch countDownLatch)
    {

        BlockingQueue<Integer> blockingQueue=new LinkedBlockingQueue<>(num);
        for(int i=0;i<producerNum;i++)
        {
            new Thread(new Producer(blockingQueue,num/producerNum,countDownLatch)).start();
        }
        for(int i=0;i<consumerNum;i++)
        {
            new Thread(new Consumer(blockingQueue,num/consumerNum,countDownLatch)).start();
        }


    }

    public static void main(String[] args) {
        CountDownLatch countDownLatch=new CountDownLatch(6);
        long s=System.currentTimeMillis();
        excute(2,4,1000000,countDownLatch);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println((double) (System.currentTimeMillis()-s)/1000);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值