Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Is using a static pre-created exception object in a throw statement thread-safe?

Reply
Thread Tools

Is using a static pre-created exception object in a throw statement thread-safe?

 
 
Shailender Bathula
Guest
Posts: n/a
 
      03-18-2005
Hi all,

Can anyone please confirm if using a static, pre-created Exception
object in a throw statement across multiple threads, thread-safe? I
have a need to use a pre-created Exception to improve the performance.
I use pre-created exception in production mode, where as new Exception
object in debug mode.

Line 14 in the sample test program below highlights this. Is Line 14,
thread-safe?

1 public class StaticPreCreatedExceptionTest implements Runnable
{
2 private static final Exception ex = new Exception("static
pre-created exception");
3
4 private String tid = null;
5
6 public StaticPreCreatedExceptionTest(String tid) {
7 this.tid = tid;
8 }
9
10 public void run() {
11 while ( true ) {
12 try {
13 // is this thread safe?
14 throw ex;
15 }
16 catch (Exception e) {
17 System.out.println(tid + ": caught exception");
18 }
19 }
20 }
21
22 public static void main(String args[]) throws Exception {
23 int MAXTHREADS = 5;
24
25 Thread threads[] = new Thread[MAXTHREADS];
26 for ( int i = 0; i < MAXTHREADS; i++ ) {
27 threads[i] = new Thread(new
StaticPreCreatedExceptionTest("t" + i));
28 threads[i].start();
29 }
30
31 for ( int i = 0; i < MAXTHREADS; i++ ) {
32 // wait until all threads are done
33 threads[i].join();
34 }
35 }
36 }

I thought it was thread-safe, but current thread stack trace of the
Java core dumps in my software always points to the "throw" statement.
So wanted to confirm from someone.

Thanks,
Shailender

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      03-18-2005
Shailender Bathula <(E-Mail Removed)> wrote:
> Can anyone please confirm if using a static, pre-created Exception
> object in a throw statement across multiple threads, thread-safe? I
> have a need to use a pre-created Exception to improve the performance.
> I use pre-created exception in production mode, where as new Exception
> object in debug mode.
>
> Line 14 in the sample test program below highlights this. Is Line 14,
> thread-safe?


Yes, it should be. The same exception may be thrown by several
different threads at the same time without difficulty. However, if you
modify the state of the exception object, then you may run into
problems.

Incidentally, if creating an Exception object is a performance-critical
operation in your code, you have VERY serious problems that you ought to
resolve immediately. Optimizing the process of throwing the exception
is not to right way to solve those problems. Fixing your abuse of
exceptions would be better.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      03-18-2005

"Shailender Bathula" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi all,
>
> Can anyone please confirm if using a static, pre-created Exception
> object in a throw statement across multiple threads, thread-safe? I
> have a need to use a pre-created Exception to improve the performance.
> I use pre-created exception in production mode, where as new Exception
> object in debug mode.
>
> Line 14 in the sample test program below highlights this. Is Line 14,
> thread-safe?
>
> 1 public class StaticPreCreatedExceptionTest implements Runnable
> {
> 2 private static final Exception ex = new Exception("static
> pre-created exception");
> 3
> 4 private String tid = null;
> 5
> 6 public StaticPreCreatedExceptionTest(String tid) {
> 7 this.tid = tid;
> 8 }
> 9
> 10 public void run() {
> 11 while ( true ) {
> 12 try {
> 13 // is this thread safe?
> 14 throw ex;
> 15 }
> 16 catch (Exception e) {
> 17 System.out.println(tid + ": caught exception");
> 18 }
> 19 }
> 20 }
> 21
> 22 public static void main(String args[]) throws Exception {
> 23 int MAXTHREADS = 5;
> 24
> 25 Thread threads[] = new Thread[MAXTHREADS];
> 26 for ( int i = 0; i < MAXTHREADS; i++ ) {
> 27 threads[i] = new Thread(new
> StaticPreCreatedExceptionTest("t" + i));
> 28 threads[i].start();
> 29 }
> 30
> 31 for ( int i = 0; i < MAXTHREADS; i++ ) {
> 32 // wait until all threads are done
> 33 threads[i].join();
> 34 }
> 35 }
> 36 }
>
> I thought it was thread-safe, but current thread stack trace of the
> Java core dumps in my software always points to the "throw" statement.
> So wanted to confirm from someone.
>
> Thanks,
> Shailender
>


Yes.
However, your statement, " I have a need to use a pre-created Exception to
improve the performance." is called an anti-pattern (where you mean to
achieve something, but you actually do the opposite). The antipattern has
been "dubbed" premature optimization.
In any case, object creation is cheap albeit common misconception to the
contrary.

--
Tony Morris
http://xdweb.net/~dibblego/


 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      03-18-2005
"Shailender Bathula" <(E-Mail Removed)> writes:

> Can anyone please confirm if using a static, pre-created Exception
> object in a throw statement across multiple threads, thread-safe?


As others have pointed out: Yes, as long as you don't change the state.

However, a word of warning: People often like the stack trace to be
useful in debugging: Your static exception will not be, since it's
unrelated to where it was thrown.

> I have a need to use a pre-created Exception to improve the
> performance.


Exceptions should be relatively rare (hence the name), so a
microsecond here or there is no preformance hit.
 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      03-18-2005
Shailender Bathula wrote:
> using a static, pre-created Exception
> object in a throw statement


The only remotely valid reason I can think of
would be an OutOfMemoryException
because the VM might not be able to create one if it is OOM,
or if it can the new OOME might be incomplete
(could be missing the stack trace array).

Could work as a quick debug-aid hack. Maybe.


> need to use a pre-created Exception to improve the performance.


Well, at least we got a good laugh out of that one.
 
Reply With Quote
 
Shailender Bathula
Guest
Posts: n/a
 
      03-23-2005
Thank you all for your responses. Point against using pre-created
exception is taken.

 
Reply With Quote
 
Shailender Bathula
Guest
Posts: n/a
 
      03-23-2005
Thanks for all your responses. Point against using pre-created
exceptions is taken.

Thomas Schodt wrote:
> Shailender Bathula wrote:
> > using a static, pre-created Exception
> > object in a throw statement

>
> The only remotely valid reason I can think of
> would be an OutOfMemoryException
> because the VM might not be able to create one if it is OOM,
> or if it can the new OOME might be incomplete
> (could be missing the stack trace array).
>
> Could work as a quick debug-aid hack. Maybe.
>
>
> > need to use a pre-created Exception to improve the performance.

>
> Well, at least we got a good laugh out of that one.


 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      04-05-2005
Shailender Bathula wrote:
> Can anyone please confirm if using a static, pre-created Exception
> object in a throw statement across multiple threads, thread-safe? I
> have a need to use a pre-created Exception to improve the performance.


> I use pre-created exception in production mode, where as new Exception
> object in debug mode.


If your design is littered with trivial exceptions
you can disable exception stack trace generation
-XX:-StackTraceInThrowable

Not nice. But if you cannot change the design, this certainly beats
having different code for dev & prod.
 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      04-12-2005
Thomas Schodt wrote:
> If your design is littered with trivial exceptions
> you can disable exception stack trace generation
> -XX:-StackTraceInThrowable
>
> Not nice. But if you cannot change the design, this certainly beats
> having different code for dev & prod.


It appears Tiger (in -server mode) will do something like this for
exceptions that happen repeatedly, controlled by
-XX:+OmitStackTraceInFastThrow
-XX:-OmitStackTraceInFastThrow
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To throw or to throw not? Emanuele D'Arrigo Python 6 11-15-2008 04:12 PM
Throw Access is Denied Exception when Creating object RC ASP .Net 0 11-20-2005 06:09 AM
Throw Access is Denied Exception when Creating object RC ASP .Net Security 0 11-20-2005 06:03 AM
JNI's throw new does not throw an exception yarona@m-sys.com Java 15 09-08-2005 08:36 AM
Throw Exception Vs Throw New Exception Kerri ASP .Net 2 10-27-2003 02:13 PM



Advertisments