如何在java中创建一个每5秒运行一次后台的线程

时间:2015-05-03 19:07:31

标签: java multithreading

我有一个代码可以在值超过阈值时创建警报,并希望该代码每5秒在后台运行一次。

这是我的代码:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String Name = request.getParameter("alarmName");
    String Description = request.getParameter("desc");
    String Metric = request.getParameter("name");
    String threshold = request.getParameter("threshold");
    String sign = request.getParameter("sign");

     response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       // List<String> lst = new ArrayList<String>();
        System.out.println("Hello" +Name);
        System.out.println(Description);
        System.out.println(Metric);
        System.out.println(threshold);
        System.out.println(sign);


        try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
        PreparedStatement ps = con.prepareStatement("Insert into alarmdetails(Name, Description, Metric, threshold,sign) values(?,?,?,?,?)");
        System.out.println(ps);
        ps.setString(1,Name);
        ps.setString(2,Description);
        ps.setString(3,Metric);
        ps.setString(4,threshold);
        ps.setString(5,sign);
        ps.executeUpdate();   

        if(Metric.equals("cpuUsage"))
        {   
            if(sign.equals("greaterthan"))
            {
                System.out.println("reached here3");
                PreparedStatement ps1 = con.prepareStatement("select CPU_USAGE from metrics WHERE CPU_USAGE > (SELECT threshold from alarmdetails WHERE Name='"+Name+"')");
                ResultSet rs1 = ps1.executeQuery();

                if(rs1.next())
                {
                    System.out.println("Alert, CPU Usage greater than threshold");
                }
                else
                {
                    System.out.println("All good");
                }
            }
        } catch (Exception e)
        {
        System.out.println(e);
        }

        String url = "Homepage.jsp";

        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
        dispatcher.forward(request, response);

      }

      }

我想在一个每5秒运行一次的线程中运行if(Metric.equals(cpuUsage))....中的代码。

我以前没有自己实现过线程,任何帮助都会非常感激。感谢。

1 个答案:

答案 0 :(得分:3)

尝试使用具有指定延迟的ScheduledExecutorService。

function ProjectFolder() {
  this.files = [];
  Object.defineProperty(this, 'size', {enumerable: true, get: function() {
    return this.files.length;
  }});
}

function GithubProjectFolder() {
  this.files = ['.gitignore', 'README.md'];
}
GithubProjectFolder.prototype = new ProjectFolder();

var project1 = new ProjectFolder();
JSON.stringify(project1);
// output:  {"files":[],"size":0}
// size is present

var project = new GithubProjectFolder();    
JSON.stringify(project);
// output:  {"files":[".gitignore","README.md"]}
// size is absent

这里,MyRunnable类会实现Runnable,它的run方法会有你的处理代码,而不用担心时间延迟。