多线程报刊订阅java实现

//SharePool.java
package cn.campsg.java.experiment.entity;

import java.util.ArrayList;
import java.util.List;

public class SharePool {
    private List<String> pool=new ArrayList();
    private final int MAX=15;

    public List<String> getPool(){return pool;}
    public void setPool(List<String> pool) {this.pool = pool;}

    public SharePool() {
        super();
    }

    public void produce(String infor) {
        synchronized (this) {
            if (pool.size() == MAX) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("订阅请求队列已满,等待系统处理订阅请求中......");
            }
            else {
                pool.add(infor);
                System.out.println("订阅者@" + Thread.currentThread().getName() + ":订阅《" + infor + "》申请已提交.当前订阅数量为:" + pool.size());
                try{
                    notify();
                }
                catch(Exception e )
                {
                    e.printStackTrace();
                }
            }
        }
    }

    public void consume() {
        synchronized (this) {
            if (pool.size() == 0) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("处理者@" + Thread.currentThread().getName() + "暂无订阅请求信息,等待中......");
            }
            else {
                String infor = pool.remove(0);
                System.out.println("处理者@" + Thread.currentThread().getName() + ":处理《" + infor + "》订阅已完毕。尚待处理订阅数量为:" + pool.size());
                try{
                    notify();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

//Producer.java
package cn.campsg.java.experiment.entity;

public class Producer implements Runnable {
    private SharePool pool;
    private int count;

    public Producer() {super();}

    public Producer(SharePool pool, int count) {
        super();
        this.pool = pool;
        this.count = count;
    }

    public void run() {
        String infor = "报刊杂志";
        int counts = 0;
        System.out.println("订阅者@" + Thread.currentThread().getName() + ":订阅" + count + "份。");
        while (count > 0) {
            counts++;
            System.out.println("订阅者@" + Thread.currentThread().getName() + " 在提交第" + counts + " 份订阅申请。");
            pool.produce(infor + counts);
            count--;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("订阅者@" + Thread.currentThread().getName() + "->完成订阅。");
    }
}

//Consumer.java
package cn.campsg.java.experiment.entity;

public class Consumer implements Runnable {
    private SharePool pool;

    public Consumer() {
        super();
    }

    public Consumer(SharePool pool) {
        super();
        this.pool = pool;
    }

    public void run() {
        int counts = 1;
        while(true)
        {
            if(counts <= 15) {
                System.out.println("处理者@" + Thread.currentThread().getName() + ":处理第" + counts + "份订阅。");
                this.pool.consume();
            }else
            {
                System.out.println("本线程完成订阅处理量,即刻退出。处理者@" + Thread.currentThread().getName());
                break;
            }
            counts++;
        }
    }
}

//MainClass.java
package cn.campsg.java.experiment;

import cn.campsg.java.experiment.entity.Consumer;
import cn.campsg.java.experiment.entity.Producer;
import cn.campsg.java.experiment.entity.SharePool;

public class MainClass {
    public static void main(String[] args) {
        SharePool pool = new SharePool();
        for (int i = 1; i <= 5; i++) {
            new Thread(new Producer(pool, i)).start();
        }
        new Thread(new Consumer(pool)).start();
    }
}


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值