Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

my first time using threads  RSS feed

 
billo bailey
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I have a program that uses multiple threads which is causing me some problems. The program is meant to mimic the delivery,dispatch of materials to and from a location. In the location there are bins that contain the materials, so i have a Bin class set up with two methods add and remove which are both synchronized. My problem is that instead of the objects sharing the generic bin they seem to be using different instances all together. For example if the Delivery thread adds materials the bin is incremented, however if the dispatch removes materials the bin is decremented but not from the same point as the previous increment. Here's what I have, it might be easier to understand. I have a feeling its a pretty straightforward problem but I just can't see it.



Thanks for any help or suggestions.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two possible problems:

1. You are only checking to see if the bin is at full capacity or empty -- not checking whether the bin can hold the amount. If you try to add 5, but only have room for 2, you will exceed capacity. Likewise, if you have 2, but try to take 5, you will succeed in having negative amounts.

2. You are accessing the bin instance variable directly outside of the synchronized methods. I would suggest making the bin variable private -- and providing a synchronized getter for it.

Henry
 
billo bailey
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
Thanks for the swift reply, I've altered my Bin class as you suggested making bin private and adding a synchronized getBin() method; aswell as conditional statements for the deliveryAmount and the dispatchAmount. Despite this I still seem to be in the same place. The threads don't seem to be interleaved at all, and they still seem to be accessing different instances of bin!! Any further suggestions, I'm at a loss myself.

Here's the altered code,



I've altered the other two classes appropriately also.

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry makes some good points, but missed the fact that the various Bin objects are member variables in Materials; therefore each instance of each subclass of Materials gets its own set of Bins. You want all the threads to share a single set of Bins.

You have two architectural options: either make the Bin variables static in Materials, or make the Runnables not extend Materials, but instead share a single instance of the Materials class, passed as a constructor argument to each of the three classes by main(), then stored in an instance variable of each class. The latter is a better architecture, but of course the former is a very trivial change.
 
billo bailey
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I just spotted that myself, I took the lazy option and it worked a treat ( I will do it as you suggested though). One further concern is that the output seems to me to be quite predictable, ie it goes from 0 - 10 and then from 10 - 0 again, sequencially, repeatedly. I assumed it would jump around like a kid in a candy store. Am I still missing something.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!