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

S

Shailender Bathula

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 = new Thread(new
StaticPreCreatedExceptionTest("t" + i));
28 threads.start();
29 }
30
31 for ( int i = 0; i < MAXTHREADS; i++ ) {
32 // wait until all threads are done
33 threads.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
 
C

Chris Smith

Shailender Bathula said:
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
 
T

Tony Morris

Shailender Bathula said:
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 = new Thread(new
StaticPreCreatedExceptionTest("t" + i));
28 threads.start();
29 }
30
31 for ( int i = 0; i < MAXTHREADS; i++ ) {
32 // wait until all threads are done
33 threads.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.
 
T

Tor Iver Wilhelmsen

Shailender Bathula said:
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.
 
T

Thomas Schodt

Shailender said:
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.
 
S

Shailender Bathula

Thank you all for your responses. Point against using pre-created
exception is taken.
 
S

Shailender Bathula

Thanks for all your responses. Point against using pre-created
exceptions is taken.
 
T

Thomas Schodt

Shailender said:
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.
 
T

Thomas Schodt

Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top