Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

throwing an exception in a thread ? 1

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!

I want to let a thread throw an exception (in order to handle that exception in the parent class). But I am not able to get this to work. I tried to do something like this:

Code:
public void run() throws IOException
{
    ...
}

But the compiler says that I am not allowed to do this. How can a thread throw an exception??

cheers

frag

patrick.metz@epost.de
 
We had this problem a while ago. You cannot override the signature of "run()" because you extend Thread in your class ...

The way we did it in the end was create a "ThreadQueueListener" which was basically just a Hashmap with methods for setting and getting the exit status of Threads. After threads finished, the object could be interrogated to see what died or succeeded.

Threre is probably a better way of doing it though ....
 
overrided method cannot throw an exception that will not throw in its super class.

In your case the run() method in the super class (Thread or interface Runnable) does not throw IOException.

 
ThreadGroup has a method for this purpose. You override ThreadGroup and extend uncaughtException.

public class MyThreadGroup extends ThreadGroup {
public MyThreadGroup(String name) {
super(name);
}
public void uncaughtException(Thread t, Throwable ex) {
System.err.println("Uncaught Exception!!! "+ t.getName());
ex.printStackTrace();
}
public static void main(String[] args) {
// test MyThreadGroup
ThreadGroup group = new MyThreadGroup("Test");
Thread th = new Thread(group, new Runnable() {
public void run() {
throw new RuntimeException("Test Exception");
}
});
th.start();
}
}
 
Step one: assuming you have a form to submit data for processing,let say the form page is form.jsp that look something like:

Code:
<%-- form.jsp --%>
....
<form action=&quot;process.jsp&quot;>
   <input type=&quot;text&quot; name=&quot;param1&quot;>
   <input type=&quot;text&quot; name=&quot;processID&quot; value=&quot;<%= (new Random()).nextLong() %>&quot;>
   <input type=&quot;submit&quot; value=&quot;submit&quot;/>
</form>
....
<%-- end of form.jsp --%>

Step 2: The form submit to process.jsp to process the data. It creates a thread to handle the process and reponse with a redirect to a page (status.jsp) that polling status from the server periodically.
Code:
<%-- process.jsp --%>
<%
   String param1 = request.getParameter(&quot;param1&quot;);
   String processID = request.getParameter(&quot;processID&quot;);
   
   session.setAttribute(&quot;pid&quot;+processID, new Integer(0));
   
   // create a Thread to process the data, pass long the session for 
   // process status logging
   ProcessThread pt = new ProcessThread(param1, processID, session);
   pt.start();
%>
<html>
<head>
<!-- redirect to status.jsp -->
<META HTTP-EQUIV=&quot;Refresh&quot; CONTENT=&quot;1; URL=status.jsp?pid=<%= processID %>&quot;>
</head>
<body>Processing.....</body>
</html>
<%-- end of process.jsp --%>

The process thread class would be something like:
Code:
public ProcessThread extends Thread {
  private HTTPSession session;
  private String processID;
  private String param1;
  
  public ProcessThread(String param1, String processID, HTTPSession session) {
     super();
     this.session = session;
     this.param1 = param1;
     this.processID = processID;
  }
  public void run() {
      // your process, assuming there is a loop here
      // that allow you report status periodically
      while (condition) {
         int percentageCompleted = ........
	 session.setAttribute(&quot;pid&quot;+processID, new Integer(percentageCompleted));
      }
  }
}

Step 3: Here is an example of the status page which refresh itself with processID as parameter to retrieve the proces status.
Code:
<%-- status.jsp -->
<%
   String processID = request.getParameter(&quot;processID&quot;);
   int completePercentage = ((Integer)session.getAttruibute(&quot;pid&quot;+processID)).intValue();
%>
<html>
<head>
<% // refresh in 2 second unless 100 percent completed
  if (completePercentage < 100) {
 %>
<META HTTP-EQUIV=&quot;Refresh&quot; CONTENT=&quot;2; URL=status.jsp?pid=<%= processID %>&quot;>
<%
   }
%>
</head>
<body>
   Process complete <%= session.getAttruibute(&quot;pid&quot;+processID) %>%
</body>
</html>
<%-- end of status.jsp --%>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top