自用

package com.gu.test2;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @ClassName Test2
 * @Description
 * @Author Gu
 * @Date 2019/11/22 11:10
 * @Version V1.0
 **/
public class TestCountDownLatch {
    private static int threadCount = 0;

    private static List<List<IpEntity>> zongData = new CopyOnWriteArrayList<>();

    private static volatile List<IpEntity> ips = new CopyOnWriteArrayList<>();

    public static String getIp() {
        return "ip";
    }

    public static void main(String[] args) throws InterruptedException {
        //判断缓冲ip集合是否大于10个ip,如果大于,开启新线程
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //如果缓冲集合大于10个ip
                if (ips.size() > 10) {
                    //创建新的集合
                    List<IpEntity> ips1 = new CopyOnWriteArrayList<>();
                    //添加10个ip
                    for (int i = 0; i < 10; i++) {
                        ips1.add(ips.get(i));
                    }
                    //统计线程数加一
                    threadCount++;
                    //总数据集合加入这个集合
                    zongData.add(ips1);
                    int threadNum = threadCount;
                    //创建新线程循环判断这个10个Ip
                    new Thread(() -> {
                        while (true) {
                            String ip = getIp();
                            for (IpEntity ipEntity : zongData.get(threadNum)) {
                                if (ip.equals(ipEntity.getIp())) {
                                    Long newTime = System.currentTimeMillis();
                                    if (ipEntity.getOutTime() < newTime) {
                                        //设置过期
                                        //删除ip
                                        ips.remove(ipEntity);
                                    } else {
                                        ipEntity.setOutTime(System.currentTimeMillis() + 2000);
                                        ips.set(ipEntity.getIndex(), ipEntity);
                                    }
                                    break;
                                }
                            }
                            //如果都为离线就退出线程
                            if(zongData.get(threadNum).size() == 0){
                                zongData.remove(threadNum);
                                return;
                            }
                        }
                    }, "工作线程" + threadCount).start();
                    //情况缓冲集合ip
                    for (int i = 0; i < 10; i++) {
                        ips.remove(i);
                    }
                }
            }
        }, "ipsCountIs10").start();
        //缓冲主线程
        while (true) {
            //获取Ip
            String ip = getIp();
            boolean flag = true;
            for (IpEntity ipEntity : ips) {
                if (ip.equals(ipEntity.getIp())) {
                    flag = false;
                    Long newTime = System.currentTimeMillis();
                    if (ipEntity.getOutTime() < newTime) {
                        //设置过期
                        //删除ip
                        ips.remove(ipEntity);
                    } else {
                        //更新时间戳
                        ipEntity.setOutTime(System.currentTimeMillis() + 2000);
                        ips.set(ipEntity.getIndex(), ipEntity);
                    }
                    break;
                }
            }
            if (flag) {
                //如果没有此Ip就添加
                ips.add(new IpEntity(ip, System.currentTimeMillis() + 2000, ips.size()));
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值