
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 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. If your package is something other than "com.main", replace com.main with the name of your package.
<servlet>
<servlet-name>HungThread</servlet-name>
<servlet-class>com.main.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/context_root/HungThread. The page should load endlessly.
Did you find this article helpful?
If so, consider buying me a coffee over at