Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Multi-threading Question about network query

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have just multi-tested threading of one of my functionality in the network section called bookRoom. Thankfully I have not endured deadlock, but am aware of the nature that threads are issued, i.e. for example thread 23 could run before thread 16. I'm wondering then in a real-world what would happen if say 50 people booked the same room at the same time. I'm using RMI but I still reckon that I could run into the multi-threaded issue discussed earlier ie. the correct customer might not be able to book the room. Is the only way around this to synchronize the business methods in the network? Does this guarantee that the first person who tried to book the room will be guaranteed to book? I have already synchronized the data class methods. Would appreciate people's views about this as I am new to project development.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

Ofcourse your concerns are reasonable and you should try to avoid this. I have followed the above steps to resolves this.

The interface which is the gateway between client and sever has the following method

public Hotel synchronizeHotel(Hotel hotel, long userId)
Scenario:
1. Thread A requests lock for record 1 by setting the hotel's status to Lock
2. Server responds back with the updated hotel
3. Client business logic checks whether the returned Hotel has been booked by other user [We suppose that is not]
4. Thread A is now holding the lock and the user is performing changes
5. Thread A proceeds with the booking by invoking again the same method by changing the hotel's flag to update and release the lock for the record

1. Thread B requests lock for record 1 by setting the hotel's status to Lock
2. Thread B is waiting for Thread A to release resources
3. Server responds back with the updated hotel (Including the changes that were made from Thread A)
4. Thread B check whether it has been booked, if not it proceeds with the booking by invoking again the same method by changing the hotel's flag to update. If it is booked the client business logic prevent from rebooking

In this scenario I assume that thread A access first the method
Between calls I ensure that a fresh copy is returned to the client in order to avoid data corruption as you mentioned, although I have found out that I have made a silly mistake in my implimentation.. I hope that it will not cost me.. Anyway when the results are out i will know

Regards,
Nicolas
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic