Results 1 to 6 of 6

Thread: Executing a task asynchronously

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Executing a task asynchronously

    Hello,
    I wanted to use the api competitor for executing a task asynchronously, it is actually sending the mail after sending a form http. such as sending mail may take some time, I would return to the page and allow the sending of mail carried in a separate process, here is my code:
    Code:
    Executor etor = Executors.newSingleThreadExecutor();
    etor.execute(new Runnable() {
       Public void run() {
    MailManager.confirmCreation(Instance);
      }
    });
    Last edited by TechGate; 03-02-2010 at 02:11 AM.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Executing a task asynchronously

    Hello,
    I think the risks are not related to Executors in themselves, but rather to your code. Like any elements that could be used from multiple threads, ensure that everything is thread safe. Clearly he / she should have your code and well on all the objects that are shared by your method (the parameter "instance" to start, but also any static data ...) I think you should think of an alternative for this.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Executing a task asynchronously

    Hi,
    I think the risk is possible flooding when the threads providers give more tasks to process the thread executor can process sequentially, the queue grows bigger. But it is a particular case depends on the nature of tasks. Yes, even I agree at the above post, as he has mentioned you should look for an .alternative solution for this

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Executing a task asynchronously

    Hello,
    You must make sure that all this is thread-safe, meaning that all the shared elements are:
    - use of thread-safe manner (see class documentation).
    - immutable (String, Integer, etc..), because it implies the next thread-safe.
    - manually synchronized with your code.
    If all of this is implemented in your code, then to some extent your code will be safe.

  5. #5
    Join Date
    Dec 2009
    Posts
    263

    Re: Executing a task asynchronously

    Hello,
    So this is not a solution to implement without taking into account the rest of the code, the goal was just returning control to the user and let the mail is processed by another thread. I must declare functions synchronized change in StringBuffer, StringBuilder and a little review of the principles of synchronization. If you guys can suggest me some other alternative then it will be appreciated. Thanks in advance.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Executing a task asynchronously

    Hello,
    It depends heavily on your code and how you share data. Every sync is not necessarily the best solution either. You can also avoid the maximum sharing of data to work on local objects up to. There are many ways to do it, it depends on your needs, that is how you want your software or application to work. So, just read on this and then think which logic you will apply to your code.

Similar Threads

  1. Replies: 3
    Last Post: 17-01-2014, 10:37 AM
  2. Successor task starts on same day predecessor task ends
    By Riverrock in forum Microsoft Project
    Replies: 3
    Last Post: 03-01-2014, 11:59 AM
  3. No desktop icons, task manager, mouse or task bar on start up
    By night-13 in forum Operating Systems
    Replies: 2
    Last Post: 07-12-2011, 10:44 PM
  4. Replies: 5
    Last Post: 29-09-2011, 01:03 PM
  5. Replies: 7
    Last Post: 31-07-2011, 05:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,507,846.85326 seconds with 16 queries