This week's giveaway is in the OCPJP forum.
We're giving away four copies of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) and have Paul Anilprem on-line!
See this thread for details.
Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

DBTester and DBTestRunner in Max's book

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This will probably only make sense if you're using Max'x book, so please bear with me...

I've set up the example project (Denny's DVDs) in Max's book and have compiled and run it successfully (both locally and in RMI mode). I'm currently looking at the testing code in the sampleproject.test package.

I've run the sample test as provided and the results look fine i.e. 5 rentals, 10 returns, number of available copies has increased by 5. I then jacked up the numbers, still fine.

However when I interspersed returns and rentals a la...



I get anomolous results i.e. number of available copies increases? Has anybody noticed this before? Is there a flaw in the multithreading code in Denny's?

Thanks,
A.

[Andrew: removed indentation on source code as it is not needed, and the screen width was annoying me]
[ August 04, 2005: Message edited by: Andrew Monkhouse ]
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alan,

I believe the problem is that even though you have interspersed your rentals and returns, these are threads at the control of the JVM thread scheduler. So there is no guarantee that a return will happen before a rental.

When I look at the output of a test run with your code, I see:We start of with lines 1,2 and 3 all returning rentals (since copies in stock are incrementing). Line 4 is our first rental, followed by a return, followed by a rental, followed by several returns.

Skipping forward to line 18 we see a return bringing our copies in stock up to 7. We then follow with 13 returns (up to line 31). However the setRented() method of the DVD class will not allow negative rented numbers:With the result that lines 26 - 31 show that copies in stock remain at zero. But with the threads returning the DVDs that previously did not exist the final number of DVDs in stock is very likely to end up greater than the starting value.

A better test would be for DBTester to do both the rental and the return (with a random delay between the rental and the return while holding zero locks), but only returning the DVD if it was allowed to rent in the first place (or alternatively, if it was unable to rent the DVD, wait for a while and try again). DBTestRunner would also have to be modified to accomodate the new DBTester.

I leave these modifications as an exercise for you .

Regards, Andrew
 
Alan Mac
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,

Thanks for the reply.


I believe the problem is that even though you have interspersed your rentals and returns, these are threads at the control of the JVM thread scheduler. So there is no guarantee that a return will happen before a rental.



I realised this, but failed to take account of the setRented() method of the DVD class not allowing negative rented numbers, as you've described.

I made a temporary change to the code to allow negative rented numbers and it now appears fine (I know changing the code to suit the test isn't exactly best practise, but it proves a point!).

Thanks again,
A.
[ August 08, 2005: Message edited by: Alan Mac ]
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alan

I know changing the code to suit the test isn't exactly best practise...

Well it is best practice if you follow the Extreme Programming Test Driven Development concepts. (However you are supposed to have a valid test that you making your code match )

Regards, Andrew
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the following error when trying to execute DBTestRunner from "SCJD Exam with J2SE 5 Platform":

Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sampleproject.sockets.DvdSocketClient.initialize(DvdSocketClient.java:300)
at sampleproject.sockets.DvdSocketClient.<init>(DvdSocketClient.java:70)
at sampleproject.sockets.DvdConnector.getRemote(DvdConnector.java:37)
at sampleproject.test.DBTester.<init>(DBTester.java:50)
at sampleproject.test.DBTestRunner.startClients(DBTestRunner.java:54)
at sampleproject.test.DBTestRunner.<init>(DBTestRunner.java:41)
at sampleproject.test.DBTestRunner.main(DBTestRunner.java:29)

Any comments would be appreciated; I have tried a number of things to fix this. I get this running in Eclipse or from the command line.

Thanks in advance!

John
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

First of all: welcome to the JavaRanch!

This is a thread from more than 4 years ago. Don't wake up a dead (zombie) thread, just create your own thread and ask your question there. Andrew is regularly visiting this forum (if he has already an internet connection after moving ), so he (or some other rancher) will be glad to answer your question.

Kind regards,
Roel
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic