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 4048650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:50:37+00:00 2026-05-20T13:50:37+00:00

I really want to abuse @Asynchronous to speed up my web application, therefore I

  • 0

I really want to abuse @Asynchronous to speed up my web application, therefore I want to understand this a bit more in order to avoid incorrectly using this annotation.

So I know business logic inside this annotated method will be handled in a separated thread, so the user wont have to wait. So I have two method that persist data

public void persist(Object object) {
    em.persist(object);
}

@Asynchronous
public void asynPersist(Object object) {
    em.persist(object);
}

So I have couple scenario I want to ask which one of these scenario is not ok to do

1. B is not depend on A 
A a = new A();
asynPersist(a); //Is it risky to `asynPersist(a) here?`
B b = new B();
persist(b);   
//Cant asynPersist(B) here because I need the `b` to immediately
//reflect to the view, or should I `asynPersist(b)` as well?

2. Same as first scenario but B now depend on A. Should I `asynPersist(a)`

3. A and B are not related
A a = new A();
persist(a); //Since I need `a` content to reflect on the view 
B b = new B();
asynPersist(b); //If I dont need content of b to immediately reflect on the view. Can I use asyn here?

EDIT: hi @arjan, thank you so much for your post, here is another scenario I want to ask your expertise. Please let me know if my case does not make any sense to u.

4. Assume User has an attribute call `count` type `int`
User user = null;
public void incVote(){
   user = userDAO.getUserById(userId);
   user.setCount(u.getCount() + 1);
   userDAO.merge(user);
}
public User getUser(){   //Accessor method of user
   return user;
}

If I understand you correctly, if my method getUserById use @Asynchronous, then the line u.setCount(u.getCount() + 1); will block until the result of u return, is it correct? So in this case, the use of @Asynchronous is useless, correct?

If the method merge (which merge all changes of u back to database) use @Asynchronous, and if in my JSF page, I have something like this

<p:commandButton value="Increment" actionListener="#{myBean.incVote}" update="cnt"/>
<h:outputText id="cnt" value="#{myBean.user.count}" />

So the button will invoke method incVote(), then send and ajaxical request to tell the outputText to update itself. Will this create an race condition (remember we make merge asynchronous)? So when the button tell the outputText to update itself, it invoke the accessor method getUser(), will the line return user; block to wait for the asynchronous userDAO.merge(user), or there might possible a race condition here (that count might not display the correct result) and therefore not recommend to do so?

  • 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-05-20T13:50:38+00:00Added an answer on May 20, 2026 at 1:50 pm

    There are quite a few places where you can take advantage of @Asynchronous. With this annotation, you can write your application as intended by the Java EE specification; don’t use explicit multi-threading but let work being done by managed thread pools.

    In the first place you can use this for “fire and forget” actions. E.g. sending an email to a user could be done in an @Asynchronous annotated method. The user does not need to wait for your code to contact the mail-server, negotiate the protocol, etc. It’s a waste of everyone’s time to let the main request processing thread wait for this.

    Likewise, maybe you do some audit logging when a user logs in to your application and logs off again. Both these two persist actions are perfect candidates to put in asynchronous methods. It’s senseless to let the user wait for such backend administration.

    Then there is a class of situations where you need to fetch multiple data items that can’t be (easily) fetched using a single query. For instance, I often see apps that do:

    User user = userDAO.getByID(userID);
    Invoice invoice = invoiceDAO.getByUserID(userID);
    PaymentHistory paymentHistory = paymentDAO.getHistoryByuserID(userID);
    List<Order> orders = orderDAO.getOpenOrdersByUserID(userID);
    

    If you execute this as-is, your code will first go the DB and wait for the user to be fetched. It sits idle in between. Then it will go fetch the invoice and waits again. Etc etc.

    This can be sped up by doing these individual calls asynchronously:

    Future<User> futureUser = userDAO.getByID(userID);
    Future<Invoice> futureInvoice = invoiceDAO.getByUserID(userID);
    Future<PaymentHistory> futurePaymentHistory = paymentDAO.getHistoryByuserID(userID);
    Future<List<Order>> futureOrders = orderDAO.getOpenOrdersByUserID(userID);
    

    As soon as you actually need one of those objects, the code will automatically block if the result isn’t there yet. This allows you to overlap fetching of individual items and even overlap other processing with fetching. For example, your JSF life cycle might already go through a few phases until you really need any of those objects.

    The usual advice that multi threaded programming is hard to debug doesn’t really apply here. You’re not doing any explicit communication between threads and you’re not even creating any threads yourself (which are the two main issues this historical advice is based upon).

    For the following case, using asynchronous execution would be useless:

    Future<user> futureUser = userDAO.getUserById(userId);
    User user = futureUser.get(); // block happens here
    user.setCount(user.getCount() + 1);
    

    If you do something asynchronously and right thereafter wait for the result, the net effect is a sequential call.

    will the line return user; block to wait for the asynchronous userDAO.merge(user)

    I’m afraid you’re not totally getting it yet. The return statement has no knowledge about any operation going on for the instance being processed in another context. This is not how Java works.

    In my previous example, the getUserByID method returned a Future. The code automatically blocks on the get() operation.

    So if you have something like:

    public class SomeBean {
    
       Future<User> futureUser;
    
    
       public String doStuff() {
          futureUser = dao.getByID(someID);
          return "";
       }
    
       public getUser() {
          return futureUser.get(); // blocks in case result is not there
       }
    
    }
    

    Then in case of the button triggering an AJAX request and the outputText rendering itself with a binding to someBean.user, then there is no race condition. If the dao already did its thing, futureUser will immediately return an instance of type User. Otherwise it will automatically block until the User instance is available.

    Regarding doing the merge() operation asynchronous in your example; this might run into race conditions. If your bean is in view scope and the user quickly presses the button again (e.g. perhaps having double clicked the first time) before the original merge is done, an increment might happen on the same instance that the first merge invocation is still persisting.

    In this case you have to clone the User object first before sending it to the asynchronous merge operation.

    The simple examples I started this answer with are pretty save, as they are about doing an isolated action or about doing reads with an immutable type (the userID, assume it is an int or a String) as input.

    As soon as you start passing mutable data into asynchronous methods you’ll have to be absolutely certain that there is no mutation being done to that data afterwards, otherwise stick to the simple rule to only pass in immutable data.

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

Sidebar

Related Questions

I really want to build a web application; something simple, maybe a database of
I've got an NSColor, and I really want the 32-bit RGBA value that it
I need to read the user's fingerprint from my application. What I really want
This may seem a bit odd, but I really need to create a workaround
I really want to get this out of my head. Please see below code:
I really want to learn more about C++. I know the basics, and I
I really want to create a stunning-looking GUI desktop application that looks like, for
I really want to do this... public class BaseController<T> where T : IPageModel, Controller
I really want to understand what is going on with my code. I have
What i really want to achieve is this--> Suppose i play an audio file(using

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.