Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What way would I go about running a singleton object that takes a long time to initialize in multiple threads?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What way would I go about running a singleton object that takes a long time to initialize in multiple threads?

    This is the problem. I have a singleton object that takes like 4 to 5 seconds to initialize, and I need to be able to make a method call to this object on multiple threads. Unfortunately this object is not thread safe. Currently I use spring to create the object at server start up and then just reference it within other spring objects so I don't have to pass around the reference to it. I make around 10 calls to this object but right now I have to do these one at a time and wait for them each to finish before making the call again.

    I want to speed this up and so the best way to do this would be for me to be able to make the method calls to the singleton object at the same time and have them run in parallel. Unfortunately since this object is a singleton and takes up a lot of memory I think I would need to run this object on separate jvms and then have each jvm receive the method call and run the code and return it.

    My question is how do I go about doing this? I think I need to use a distributed system but I am not familiar with them and so I am not sure if this is the best way or if there is an easier way. Are there any frameworks or tools that are made for this?

    Thanks in advance.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What way would I go about running a singleton object that takes a long time to initialize in multiple threads?

    Why not synchronize it? Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)
    That should postpone the other threads until whatever thread is using the object releases it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Hello, first time caller long time programmer....
    By P2C2N in forum Member Introductions
    Replies: 3
    Last Post: December 10th, 2012, 11:53 AM
  2. Replies: 2
    Last Post: October 19th, 2012, 03:32 AM
  3. Replies: 6
    Last Post: December 9th, 2011, 05:53 PM
  4. How to control the time that a function takes to execute in a loop?
    By GodspeedPR in forum Loops & Control Statements
    Replies: 4
    Last Post: July 20th, 2011, 03:37 PM
  5. Replies: 0
    Last Post: March 9th, 2011, 02:58 AM