-
Getting a thread to throw an exception
Sorry if this is in the wrong place, can a mod move it if it is?
Is it possible to get the run method inside a thread to throw an exception?
If I describe the problem maybe I can get some advice on a different way to go about this.
I have a main class class1 which creates a thread (class2) and executes some code within a timelimit. If class1 takes too long the thread should throw back an exception.
try
{
Class2 myThread = new Class2();
myThread.start();
// do some stuff here within timelimit
}
catch (Exception e)
{
e.printStackTrace();
}
The best I can do it to get the thread to print "time out" when the timelimit
is over and class1 is still executing.
The problem is, the run method of a thread cannot throw exceptions.
Is there a way around this? Is there a better to go about what I'm doing?
Thanks
jjamesis
Last edited by jjamesis; 10-12-2005 at 07:49 AM.
-
You could make a new thread that is started immediately after the start of the
myThread thread. This thread could sleep for the duration of the timelimit, after that
is could check that:
myThread!= null && myThread.isAlive()
if so it could just do a myThread.interrupt() or set a boolean that is checked regularly
by the Class2 thread signalling an exit for the thread.
This is just a suggestion, - there are many ways to get around this.
eschew obfuscation
-
As per your solution, if a second thread was made right after the first myThread, that second thread would still be created some time after the first thread. Of course this would likely be a very insubstantial time creation but nonetheless a time difference. So in effect the time for which the first myThread was supposed to run would be extended. In some cases this might be important??? though likely not in this specific case.
-
Then starting the sleeper thread should be the first step of the myTread process.
eschew obfuscation
-
Attach software www.cajoon.com to your Java environment (J2SE, J2EE) and get 'root-cause' for every exception
Similar Threads
-
Replies: 4
Last Post: 12-29-2008, 08:25 AM
-
Replies: 3
Last Post: 04-02-2007, 05:51 PM
-
Replies: 0
Last Post: 03-29-2002, 07:28 PM
-
Replies: 1
Last Post: 10-29-2000, 04:51 PM
-
Replies: 2
Last Post: 10-27-2000, 03:07 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks