Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 暂停并重新启动无限循环中的线程_Java_Multithreading - Fatal编程技术网

Java 暂停并重新启动无限循环中的线程

Java 暂停并重新启动无限循环中的线程,java,multithreading,Java,Multithreading,我需要一个如下的要求 我有一个jsp包含启用和禁用单选按钮 如果我单击enable,一个进程应该以每2或3小时一次的时间间隔执行 如果单击并提交“禁用”,则 当前正在执行的任务应暂停,直到再次启用 从jsp提交/单击(如果启用,则应执行上述步骤 继续执行) } 您的问题是什么?我需要监视服务器。当其进程>80时,我必须自动向管理员发送邮件。我的代码应该每小时持续检查服务器。我需要通过jsp启用/禁用此选项。除了时间,一切正常。你有什么问题?我需要监控服务器。当其进程>80时,我必须自动向管理员

我需要一个如下的要求

  • 我有一个jsp包含启用和禁用单选按钮
  • 如果我单击enable,一个进程应该以每2或3小时一次的时间间隔执行
  • 如果单击并提交“禁用”,则 当前正在执行的任务应暂停,直到再次启用 从jsp提交/单击(如果启用,则应执行上述步骤 继续执行)

    }


您的问题是什么?我需要监视服务器。当其进程>80时,我必须自动向管理员发送邮件。我的代码应该每小时持续检查服务器。我需要通过jsp启用/禁用此选项。除了时间,一切正常。你有什么问题?我需要监控服务器。当其进程>80时,我必须自动向管理员发送邮件。我的代码应该每小时持续检查服务器。我需要通过jsp启用/禁用此选项。除了时间,一切都在运转。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{


    //first time execution
    if(request.getParameter("lic").equals("enable") && counter==0)
    {
        t1.start();
        counter++;
        System.out.println(counter);
    }


    //if already started and clicks re-enable
    else if(request.getParameter("lic").equals("enable") && counter>0)
    {
        System.out.println("thread already started");
        t1.interrupt();
    }

    //for long pausing of thread
    else if(request.getParameter("lic").equals("disable"))
    {
        System.out.println("thread is going to pause");
        try 
        {
            Thread.sleep(20000);
            System.out.println("slept for 20 seconds");
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            System.out.println("interrupted while sleeping");
        }

    }
}
static int counter = 0;
static Thread t1 = new Thread()
{
    public void run()
    {
        System.out.println("hi thread is executed");
    }   
};