Forums Register Login

Java thread synchronization problem

+Pie Number of slices to send: Send
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?

.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1347 times.
Similar Threads
Want to convert my code in method
How to retrieve data from database every 10 minutes?
Having trouble with SELECT statement is JSP
Can you use nested sql statements in jsp??
GetAJob()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:53:58.