tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitri Colebatch <...@nuix.com.au>
Subject Re: Killing endless loop servlet - howto ? killing JVM or unload class ?
Date Fri, 13 Jul 2001 00:11:27 GMT
Hi,

On Thu, 12 Jul 2001 18:50, Endre StĂžlsvik wrote:
> How would you stop this thread?
>
> while(true);

something like this:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet 
{

    private Test test;

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException
    {
        
        String action = request.getParameter("action");
        String output = null;
        
        if ("start".equals(action))
        {
            if (test != null)
                output = "thread already running.";
            else 
            {
                test = new Test();
                test.start();
                output = "starting thread.";
            }
        }
        else if ("stop".equals(action))
        {
            if (test == null) 
            {
                output = "thread doesn't exist.";
            }
            else 
            {
                test.interrupt();
                test = null;
                output = "stopping thread.";
            }
        }
        else 
        {
            output = "unknown action.";
        }
        

        // this bit sets the content type
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        out.println(output);

    }

    private class Test extends Thread
    {
        public void run()
        {
            log("starting.");
            int i=0;
            while (true) 
            {
                log("running " + (++i));
                try
                {
                    sleep(1000);
                }
                catch (InterruptedException ie)
                {
                    log("interrupted.");
                    break;
                }

                if (isInterrupted()) 
                {
                    log("exiting.");
                    break;
                }
                
            }
            
        }
    }
}


cheers
dim


Mime
View raw message