This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes java concurrency - synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "java concurrency - synchronized block" Watch "java concurrency - synchronized block" New topic
Author

java concurrency - synchronized block

Aleksandar Mitrev
Greenhorn

Joined: Mar 21, 2008
Posts: 25
Hi everyone
I have a thread issue in my code that should not be happening - but is. So I'm trying to make some work around. I will try to explain my problems with simple code as I can - because the code that I'm experiencing the issue is big and complicated so in short the code:


So my problem begins when two thread are trying to execute-read-write changes on the same jobObject at the same time that makes mess in my application
What I need is a way that when a thread is entering "if(job.getStatus.equals("redye")){" with e.g. job object with id=1111 no other thread can enter this code for the same jobObject-1111 until the first thread is finished - leaves the if statement
I also have problems synchronizing it because the "execute" method is call with different instance of ExecuteJob
I can use ExecuteJob.class for synchronization but I will lose the point and benefits of multithreading
I'm wondering if there is something like this that can be implemented for my issue:

or maybe
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Don't use a static variable to store the thing - that static variable is used by all instances, and so you would have synchronization issues with it. But there is no need for the variable anyway - just use the method-local Job variable:

What I don't know is if the same Job instance is used by all threads, or if getDBJobsFromPath(path) creates new instances each time it is called. If it creates new instances, then we have to see more about the problem, but maybe using database locks would be best.


Steve
Aleksandar Mitrev
Greenhorn

Joined: Mar 21, 2008
Posts: 25
Thanks Steve for your quick reply
So in my case the same record is retrieved from DB-tabale job, but two different instance are been created for example:
Thread-1 gets it from DB and initialize Job job1 = readByIdFromDB("1111")
Thread-2 gets it from DB and initialize Job job2 = readByIdFromDB("1111")
so the expression job1==job2 gives false
but for job1.equals(job2) will return true
From what you are saying and I was able to understand synchronization wont going to work for me in this case. So can you tell me more about that database locks, how can I use it in my problem. I'm using postgres and I'm not rely familiar with this
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Here is the Postgres documentation on locking: http://www.postgresql.org/docs/8.2/static/explicit-locking.html You will need to match the level of locking to whe Object you want to lock, not sure if it is Row or Table. What I would think you would do is provide each Job with a reference to some Database Access Object which knows which Table/Row to lock for that Job.


Then you could use the code like:


All the other DB calls would use the same connection the lock creates, and the unlock would commit (or rollback) the transaction. But to be honest my JDBC knowledge is getting rusty and I have never used PostgreSQL, so use with care.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java concurrency - synchronized block
 
Similar Threads
Processing lots of JMS messages
ThreadPool, don't execute two jobs simultaneously if certain conditions are met
large system.in to object concurrent operations
Help me understand synchronized
form values not showing in jsp