IndexController.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.xxl.controller;
  2. import java.text.MessageFormat;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.concurrent.TimeUnit;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import com.xxl.quartz.DynamicSchedulerUtil;
  11. @Controller
  12. @RequestMapping("/job")
  13. public class IndexController {
  14. @RequestMapping("/index")
  15. public String index(Model model) {
  16. List<Map<String, Object>> jobList = DynamicSchedulerUtil.getJobList();
  17. model.addAttribute("jobList", jobList);
  18. return "job/index";
  19. }
  20. @RequestMapping("/help")
  21. public String help(Model model) {
  22. return "job/help";
  23. }
  24. private int simpleParam = 0;
  25. private ThreadLocal<Integer> tlParam;
  26. @RequestMapping("/beat")
  27. @ResponseBody
  28. public String beat() {
  29. if (tlParam == null) {
  30. tlParam = new ThreadLocal<Integer>();
  31. }
  32. if (tlParam.get() == null) {
  33. tlParam.set(5000);
  34. }
  35. simpleParam++;
  36. tlParam.set(tlParam.get() + 1);
  37. long start = System.currentTimeMillis();
  38. try {
  39. TimeUnit.SECONDS.sleep(1);
  40. } catch (InterruptedException e) {
  41. e.printStackTrace();
  42. }
  43. long end = System.currentTimeMillis();
  44. return MessageFormat.format("cost:{0}, hashCode:{1}, simpleParam:{2}, tlParam:{3}",
  45. (end - start), this.hashCode(), simpleParam, tlParam.get());
  46. }
  47. public static void main(String[] args) {
  48. Runnable runa = new Runnable() {
  49. private int simInt = 0;
  50. private ThreadLocal<Integer> tlParam = new ThreadLocal<Integer>();
  51. @Override
  52. public void run() {
  53. while (true) {
  54. try {
  55. TimeUnit.SECONDS.sleep(1);
  56. } catch (InterruptedException e) {
  57. e.printStackTrace();
  58. }
  59. if (tlParam.get() == null) {
  60. tlParam.set(0);
  61. }
  62. simInt++;
  63. tlParam.set(tlParam.get()+1);
  64. System.out.println(Thread.currentThread().hashCode() + ":simInt:" + simInt);
  65. System.out.println(Thread.currentThread().hashCode() + ":tlParam:" + tlParam.get());
  66. }
  67. }
  68. };
  69. Thread t1 = new Thread(runa);
  70. Thread t2 = new Thread(runa);
  71. t1.start();
  72. t2.start();
  73. }
  74. }