UserController.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.alvin.controller;
  2. import com.alvin.util.DateUtil;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.util.concurrent.ConcurrentHashMap;
  7. @RestController
  8. @RequestMapping("user")
  9. public class UserController {
  10. @GetMapping("age")
  11. public String test1(Integer age) {
  12. return age + "!";
  13. }
  14. public volatile ConcurrentHashMap<String, String> flags=new ConcurrentHashMap<>();
  15. @GetMapping("startPrint")
  16. public String startPrint(String flag) {
  17. flags.put(flag, flag);
  18. new Thread(() -> {
  19. while (flags.contains(flag)) {
  20. try {
  21. Thread.sleep(1000);
  22. } catch (InterruptedException e) {
  23. throw new RuntimeException(e);
  24. }
  25. System.out.println(flag + DateUtil.getEpochMilli());
  26. }
  27. }).start();
  28. return "1";
  29. }
  30. @GetMapping("cancelPrint")
  31. public String cancelPrint(String flag) {
  32. flags.remove(flag);
  33. return "ok";
  34. }
  35. }