计数器真解1

文章目录

计数器实现

package com.policemiddle.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Timer;
import java.util.TimerTask;
@RestController
@RequestMapping("/send")
public class SendMassage {
    private static int counter = 0;
    private static final long TIME_WINDOW = 60 * 1000; // 1分钟
    private static final long MAX_CALLS = 20; // 1分钟
    private static Timer timer = new Timer();

    static {
        timer.schedule(new TimerTask() {
            public void run() {
                counter = 0; // 清零计数器
            }
        }, TIME_WINDOW, TIME_WINDOW);
    }
    @GetMapping("/sendMessage")
    public Object myMethod() throws Exception {
        if (counter >= MAX_CALLS) {
            throw new Exception("方法调用次数超过限制");
        }
        counter++; // 计数器递增
        System.out.println(counter);
        // 方法的正常逻辑
        return counter;
    }
}

package com.policemiddle.controller;

import org.springframework.data.repository.init.ResourceReader;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

public class FileReaderExample {
        public static void main(String[] args) {
            // 通过ClassLoader读取resources下的文件
            //InputStream inputStream = ResourceReader.class.getClassLoader().getResourceAsStream("/123.txt");
            InputStream inputStream = ResourceReader.class.getClassLoader().getResourceAsStream("123.txt");
            Set<String> set = new HashSet<String>(); // set去重
            if (inputStream != null) {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        //System.out.println(line);
                        set.add(line);
                    }
                    for (String v : set) {
                        System.out.println(v); // 列结果
                    }
                    set.size();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}
package com.policemiddle.controller;

import com.policemiddle.pojo.Person;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AgeComparator {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("tarun", 28));
        persons.add(new Person("arun", 29));
        persons.add(new Person("varun", 12));
        persons.add(new Person("arun", 22));
//匿名内部类
//        Collections.sort(persons, new Comparator<Person>() {
//            @Override
//            public int compare(Person t, Person t1) {
//                return t1.getAge() - t.getAge();
//            }
//        });
        Collections.sort(persons, (Person t,Person t1) -> t1.getAge() - t.getAge());
        System.out.println(persons.get(0).getName());
        System.out.println(persons.subList(0,3));
    }
}

参考
参考
参考
参考
参考

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值