Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8252995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:43:18+00:00 2026-06-08T00:43:18+00:00

For measuring execution time of methods, I’ve seen suggestions to use public class PerformanceInterceptor

  • 0

For measuring execution time of methods, I’ve seen suggestions to use

public class PerformanceInterceptor {
   @AroundInvoke
   Object measureTime(InvocationContext ctx) throws Exception {
   long beforeTime = System.currentTimeMillis();
   Object obj = null;
   try {
      obj = ctx.proceed();
      return obj;
   }
   finally {
      time = System.currentTimeMillis() - beforeTime;
      // Log time
   }
}

Then put

@Interceptors(PerformanceInterceptor.class) 

before whatever method you want measured.

Anyway I tried this and it seems to work fine.

I also added a

public static long countCalls = 0;

to the PerformanceInterceptor class and a

countCalls++; 

to the measureTime() which also seems to work o.k.

With my newby hat on, I will ask if my use of the countCalls is o.k. i.e
that Glassfish/JEE6 is o.k. with me using static variables in a Java class that is
used as an Interceptor…. in particular with regard to thread safety. I know that
normally you are supposed to synchronize setting of class variables in Java, but I
don’t know what the case is with JEE6/Glassfish. Any thoughts ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:43:20+00:00Added an answer on June 8, 2026 at 12:43 am

    There is not any additional thread safety provided by container in this case. Each bean instance does have its own instance of interceptor. As a consequence multiple thread can access static countCalls same time.

    That’s why you have to guard both reads and writes to it as usual. Other possibility is to use AtomicLong:

    private static final AtomicLong callCount = new AtomicLong();
    
    private long getCallCount() {
       return callCount.get();
    }
    
    private void increaseCountCall() {
       callCount.getAndIncrement();
    }
    

    As expected, these solutions will work only as long as all of the instances are in same JVM, for cluster shared storage is needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to use joda-time library for easier measuring the execution time of my
I am measuring the execution time of methods that open and close a database,
I have found some code on measuring execution time here http://www.dreamincode.net/forums/index.php?showtopic=24685 However, it does
Ok, I have this basics PHP logic made for measuring execution time in PHP,
I use this code for measuring computing time of my C program: clock_t start
How can I measure the execution time of a query without measuring the time
I have seen other user posts which show Stopwatch measuring time spent in Thread.Sleep(5000)
I wanna measure the performance to my database by measuring the time taken to
I found an example of QueryPerformanceCounter, http://advancedcppwithexamples.blogspot.com/2009/08/measuring-elapsed-time-in-c-using_21.html The example measures a sleep of 100ms
I am profiling some AS code by measuring wall clock time. In order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.