If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Threaded View

Threading: wait /notify...please help !

Hi guys...
my question is about implementation of wait and notify to simulate threading...here is the Problem description:
I have an application to simulate 2 trains travelling in the same direction on a loop from station, through a tunnel and back to the station ;normaly on 2 seperate tracks.however both tracks pass through the tunnel wich has one single track: so there should be one train at a time in the tunnel...

what i need is to rewrite method useTunnel() given below so that train 1 and 2 are allowed to enter tunnel in strict rotation:train 2 should be allowed to enter first then train 1 then 2 and so on...method useTunnel should contain code to enforce the ordring of which train can enter at a given time...for this we should use wait and notify and maybe change the Tunnel constractor...how can i do this?

public void useTunnel (Train t) {
/* the tunnel is a shared resource
between Train 1 and 2 .so we should use
a synchronized block to lock the object "Tunnel"
from being accessed simultaneousely by Train 1 and 2
*/
synchronized(this){