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

Discussion in 'Java' started by Shailender Bathula, Mar 18, 2005.

  1. 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
     
    Shailender Bathula, Mar 18, 2005
    #1
    1. Advertising

  2. Shailender Bathula

    Chris Smith Guest

    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.
    >
    > 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
     
    Chris Smith, Mar 18, 2005
    #2
    1. Advertising

  3. Shailender Bathula

    Tony Morris Guest

    "Shailender Bathula" <> wrote in message
    news:...
    > 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.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Mar 18, 2005
    #3
  4. "Shailender Bathula" <> 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.
     
    Tor Iver Wilhelmsen, Mar 18, 2005
    #4
  5. Re: Is using a static pre-created exception object in a throw statementthread-safe?

    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.
     
    Thomas Schodt, Mar 18, 2005
    #5
  6. Thank you all for your responses. Point against using pre-created
    exception is taken.
     
    Shailender Bathula, Mar 23, 2005
    #6
  7. 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.
     
    Shailender Bathula, Mar 23, 2005
    #7
  8. Re: Is using a static pre-created exception object in a throw statementthread-safe?

    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.
     
    Thomas Schodt, Apr 5, 2005
    #8
  9. Re: Is using a static pre-created exception object in a throw statementthread-safe?

    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
     
    Thomas Schodt, Apr 12, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    13,035
    Kevin Spencer
    Oct 27, 2003
  2. Replies:
    15
    Views:
    7,567
    Roedy Green
    Sep 8, 2005
  3. Gabriel Rossetti
    Replies:
    0
    Views:
    1,332
    Gabriel Rossetti
    Aug 29, 2008
  4. Emanuele D'Arrigo

    To throw or to throw not?

    Emanuele D'Arrigo, Nov 14, 2008, in forum: Python
    Replies:
    6
    Views:
    323
    Emanuele D'Arrigo
    Nov 15, 2008
  5. John Nagle
    Replies:
    5
    Views:
    475
    John Nagle
    Mar 12, 2012
Loading...

Share This Page