Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sam Saha
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3398
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Areeda
Ranch Hand
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic