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 Java thread synchronization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Java thread synchronization problem" Watch "Java thread synchronization problem" New topic
Author

Java thread synchronization problem

catrine ohm
Greenhorn

Joined: Mar 07, 2012
Posts: 13

Hi all,

I have a problem about thread synchronization.I have 3 threads.These threads runnable class.




-----------------------------------------------------------------------------------------------------------------------
This class create thread and run method go database table and get one row.Then update one column of this row.


----------------------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------------------------

My problem is that first thread get one database record and update one row.My getAJob() method is synchronize method.But thread 1 get first row and update row, thread2 get also same row and update row.thread 3 get same row and update.But my method synchronize method.If thread 1 is in getAjob() method,thread 2 and thread3 must wait I think.I want to that for example thread1 run and run getAjob() method,thread 2 and thread 3 wait in the meantime.Then when thread1 exit getAjob() method thread2 run getAjob() method.How can I do this?How can I solve my problem?

.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
catrine ohm wrote: . . . It is urgent,please!!!
No, it isn’t. We don’t say urgent here. I think this question is too difficult for “beginning”, so I shall move it.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

You're creating a new instance of DBProcess in ScheduleThread constructor. That way each of the threads has its own instance of DBProcess. Synchronized non-static methods disallow concurrent call of methods on the same instance, but synchronized methods of different instances can actually execute simultaneously.

Try creating just one instance of the DBProcess class (you're already creating one outside of the threads) and passing it to individual threads as a parameter to the constructor so that all your threads use the same instance. This should work as you need.
catrine ohm
Greenhorn

Joined: Mar 07, 2012
Posts: 13

Hi Martin,

Try creating just one instance of the DBProcess class (you're already creating one outside of the threads) and passing it to individual threads as a parameter to the constructor so that all your threads use the same instance.




this code line is enough to work as I need.Is it true? or is there any missing code?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Did you try out the code? I'd say it will not work, you'll get NullPointerException. I can't say for sure, I haven't seen all of your code, but you can try and if I'm right, try to figure out why is that happening.

I'll try to rephrase what you need to do: you need to create one instance of the DBProcess class and have all threads use this single instance to do their database work. Now this can be done in several ways, but I'd suggest you to give the common instance of the DBProcess class to individual ScheduleThread instances so they can use it. You might add a parameter to the constructor and pass the DBProcess instance in that parameter, or you could create a setter method to set the value of the dbprocess field after the ScheduleThread instance is created.

Meanwhile, you might want to read the Oracle's Concurrency tutorial. I forgot to add it to my previous post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java thread synchronization problem