File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Problem faced in Multithreading Environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Problem faced in Multithreading Environment" Watch "Problem faced in Multithreading Environment" New topic
Author

Problem faced in Multithreading Environment

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 132
We have a third party jar used in our code. There is a class file in the jar which contains a STATIC variable defined in it.

Now, there is one application which is using this jar and modifying the value of this STATIC variable in multithreaded environment. We don't have access to modify the code of this jar.

Also, we are accessing the same jar file and value of that STATIC variable to implement some logic. Now, what is happening is that since our application and other application is running in parallel in multithreaded environment, so, while we are in between our code execution of some logic (say a block of 5 statments) which depend upon the value of that static variable, the processor executes the other application code in between and modifies the value of that variable and hence, breaking our code.

Please suggest us how to resolve this issue.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 206
To elaborate the problem more clearly, let me put it this way ...

We have 2 parallel threads running that connect to MQ server.

There are get and send methods which use the properties like this..




Every time, I try to get the message from MQ, I do set these properties before calling my get/send method.
But, there are chances, that by the time my control reaches the get method, the processor has been preemptied and assigned to the other thread which might again change the properties, and when my control again reaches my first thread get method, it'll try to get message from the changed properties.

I hope, I am able to make my point clear.

Please let me know, if I am not clear.


Is it possible to excute the bunch of these 4 statements as a transaction, I mean if a thread starts executing it should not be preemptied by other thread until it executes all the 4 statements in the block.?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3438
    
  47

Yogesh, are you elaborating on somebody else's problem?

The resolution of your problem is quite easy: put the five lines into an synchronized block, and use a single object to synchronize on. I would be tempted to synchronize on MQEnvironment.class, since (presumably) it owns the static variables which are causing you problems.

See also Java concurrency tutorial. You might also want to find another, thread safe library.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9327
    
110

Just a note though, to what Martin mentioned - If any other part of your code or the third party application's code doesn't use the same object to synchronize on or doesn't use synchronization at all for accessing those properties, then it's back to square one. Looking at that code, it's a bit weird why those properties are all static and instead couldn't have been designed to pass along a specific instance of MQEnvironment to whatever operations are using it.

Edit: Actually, I just saw that Martin has already made the point about the library not being a good one.

[My Blog] [JavaRanch Journal]
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3438
    
  47

I've assumed that the problematic static properties are not used outside of this synchronized block. If they are, this is not going to work, indeed.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 206
What I understand from Martin's explanation is

I should do something like



Is this what you were trying to say?

Yes, you are right, if the other thread is not using synchronized block then its back to square one.
Indeed this couldn't be the best library, but i believe this is how this library works.

If anyone has knowledge about MQ, can please comment how do they handle this scenario

The library name is com.ibm.mq.jar (perhaps).

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9327
    
110

Yogesh Gandhi wrote:

If anyone has knowledge about MQ, can please comment how do they handle this scenario

The library name is com.ibm.mq.jar (perhaps).



A quick google search indicates that your application is using the wrong API and there's an alternate API for this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem faced in Multithreading Environment
 
Similar Threads
Design Connection with respect to DataBase Connection
Singleton clash inside web application server?
How can we allow multiple values of max-heap-size
How are you using Clojure?
Loading Property files using Struts