wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Defining how many threads can access a resource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Defining how many threads can access a resource" Watch "Defining how many threads can access a resource" New topic
Author

Defining how many threads can access a resource

cristian zoccarato
Greenhorn

Joined: Nov 15, 2013
Posts: 7
(I know, the topic's title is not the best but I think that explaining my "problem" will make it clearer..Also, in case there's already a topic about this, please let me point to it)

I'll try to be short.
I found the classic Horse Racing code in which four horses run for 3 laps on a field. This code can provide the time each horse runs a lap and it was added the funny feature of a WaterTrough. It just gives a random time the horse uses to drink water.
This is the WaterTrough class code



So, it seems that just one horse can access this "water trough area". But, what if I would like to let 2, or 3 horses accessing the area at the same time?
Thanks in advance to whomever will help
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Take a look in the java.util.concurrent package (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-frame.html). There are a couple different means you could use to emulate this activity.

The most straightforward is probably the Semaphore. It has a count of permissions, and will block when you try to take a permission that isn't available. So on the plus side, it lets you do pretty much what you want, on the downside, it lets you take multiple permissions or release permissions from a different thread so it could be used by evil jockeys to manipulate the system.

I am sure there is a way to do it with CountDownLatch, but nothing quite as simple as Semaphore. And I can all but guarantee there are multiple other ways of doing it but these two are what stand out to me.


Steve
cristian zoccarato
Greenhorn

Joined: Nov 15, 2013
Posts: 7
Thanks Luke!
I think I considered Semaphore but for unknown reasons I never went for it. So, i never used and tried to do something with it. Do you think it is correct?



I added both acquire() and release() into the getDrink() method. It "seems" working but I'm really not sure about...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I think you should remove the synchronized keyword from the method - that would limit access to just one, which isn't what you want. After that it looks okay, but you should test again, and keep some logging / output to track who gets to what part of the method when. And really try to hammer it (have 5, 10, 100, 1000 horses all trying to get a drink. Then put a three or four in tight loops trying to access the water and make sure they all get a chance (and one horse doesn't get an unfair access to the water). Remember that we are in Computer Science - and in science there is always a lot of tests and observation happening!
cristian zoccarato
Greenhorn

Joined: Nov 15, 2013
Posts: 7
Yeah, you're right. I forgot to remove it on the code I posted here but I did on the one I compiled. It seems it works fine now. I tried adding more and more horses (up to 100) as well as for adding "seats" (or whatever kind of word fits for it) in the watertrough and it seems working...but, as you rightly suggested, I need to experiment more and do some checkings!
Thanks for your help!
C.
 
GeeCON Prague 2014
 
subject: Defining how many threads can access a resource