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

Java thread synchronization problem

 
catrine ohm
Greenhorn
Posts: 13
IBM DB2 Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
IBM DB2 Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic