• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem faced in Multithreading Environment

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic