aspose file tools*
The moose likes Java in General and the fly likes The application is working for single thread but not working multiple threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "The application is working for single thread but not working multiple threads" Watch "The application is working for single thread but not working multiple threads" New topic
Author

The application is working for single thread but not working multiple threads

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
Hi
I am working on an application which EngineFactory class file is written based on Singleton pattern and it is not thread safe. So when I run only one thread at a time the application is working fine. But when I tried running two threads in parallel at the same time the application is not working correctly. I am new in multithreaded environment. I workaround with “synchronized” to fix this issue but not success. I would really appreciate if someone can give me any idea how I can fix this issue.
The Initial code was like below:


Then I tried with “synchronized”, but no success. Here is the code:
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1977
    
    7

I don't see how synchronizing the singleton like you did will help.

What is a singleton? Every one/thread gets the same instance of the class.

What you should do is synchronize the methods the singleton have. Whether the entire method or blocks of it.

You may also want to check out the concurrency package in java.util.concurrent if you have to do read/write stuff.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 294
    
    2

Hi Sam,

I have a couple of comments that fall well short of a solution to your problem.

First of all I find it easier to user synchronized methods rather than synchronize statements see http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

Second is a question about exactly what you're trying to do.

From my reading of the code, all you're synchronizing is the creation of the singleton object but I would think that synchronizing the methods of the instance is even more important.

So my first question is why make the EngineFactory a signleton? Does not being thread safe mean multiple threads should not call a single instance or does it mean multiple instances should not be created? If it's the former each thread could have it's own instance. If it's the latter then I would suggest either modifying the source or creating a wrapper to synchronize the method calls.

Synchronizing methods is not a simple, you can do it blindly without understanding how it's used, kind of thing. In other words it's often the case that the lack thread safety is due to multiple step transactions keeping state in the object.

Consider this oversimplified example of getting data from a website



I know it's a silly example but the point I'm trying to make is that if you make DataGetter a singleton object then the three steps have to be done as an atomic operation, no other thread can access it until all three steps complete. On the other hand each thread could have one of these objects and work independently.

I know I'm guessing what your EngineFactory might do, I hope that helps a bit.

Joe

It's not what your program can do, it's what your users do with the program.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: The application is working for single thread but not working multiple threads
 
Similar Threads
Questions about the fixed "volatile" keyword
Closing a thread within a fixed thread pool
How to turn an Applet into a Standalone?
Force Landscape Printing
Strange behavior with fixed pool thread and executor