How to replicate a hung thread in Eclipse

Home > Search > How-to
  by

This assumes you have created your first dynamic web project in Eclipse. If you have not yet created a package, in the left panel of Eclipse, expand the Java Resources folder, right click on the src folder, and select New > Package. Give the package a name, such as com.main.servlet and select Finish. Right click on the newly created package and select New > Servlet. Give the servlet a name, such as HungThread, and select Finish.

If you want the thread to hang indefinitely, in the doGet method section of the markup, add the following.

while(true) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

 

If you want the thread to hang for 13 minutes, in the doGet method section of the markup, add the following.

int count = 1;
while (count < 800) {
  try {   			
    Thread.sleep(1000);
    count++; 
	} catch (InterruptedException e) {
      e.printStackTrace();
	}   		
    out.print("Thread was hung for 800 seconds (13 minutes).");
}

 

Add the following markup to your web.xml file.

<servlet>
   <servlet-name>HungThread</servlet-name>
    <servlet-class>com.jwt.servlet.HungThread</servlet-class>
</servlet>
 
<servlet-mapping>
    <servlet-name>HungThread</servlet-name>
    <url-pattern>/hungthread</url-pattern>    
</servlet-mapping>

 

You can now run the project on your server runtime in Eclipse, or export the WAR or EAR to your application server (JBoss Tomcat WebSphere), and then navigate to www.example.com/app_name/myServlet. The page should load endlessly.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments