计数器实现
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;
private static final long MAX_CALLS = 20;
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) {
InputStream inputStream = ResourceReader.class.getClassLoader().getResourceAsStream("123.txt");
Set<String> set = new HashSet<String>();
if (inputStream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
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, (Person t,Person t1) -> t1.getAge() - t.getAge());
System.out.println(persons.get(0).getName());
System.out.println(persons.subList(0,3));
}
}
参考
参考
参考
参考
参考