Reputation: 8376
I want to try out Java Semaphores, so I thought about this problem:
There's a train station and two railways are crossing, one to the east and another one to the south. So of course we should get sensors at the end of the cross for each rail which tell us when a train has crossed and the southern or eastern railway can allow its trains to start.
So far, I think sensors would be Semaphores
, Trains classes would be Threads
and I could have any sort of railway classes where I throw the trains to run.
I'm a newbie with Java Semaphores, any suggestion would be appreciated
Upvotes: 4
Views: 889
Reputation: 15758
You should only block the entry to the protected area. Put the semaphores there.
Actually, you should use one semaphore, with 1 permit, because you have 1 protectable resource. The trains (Thread
s) will acquire it before they enter, and release after they left the station.
Upvotes: 6