Java同步
我有点卡在这里,我必须编辑这段代码,以便火车必须在隧道锁前等待火车从相反方向驶出才能进入隧道,我必须使用内置的java同步条件
public synchronized void useTunnelLock(Train train)
{
System.out.println(train + " " + train.getDirection());
System.out.println(train + " exiting lock");
try
{
// occupy tunnel lock for 5s
Thread.sleep(5000);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
// swap direction of tunnel lock
direction = (direction == ASCENDING)? DESCENDING : ASCENDING;
}
I am bit stuck here, I have to have edit this code so that a train must wait in front of the tunnel lock for a train to come in the opposite direction to exit before it can enter the tunnel, i have to use java built in synchronisation condition
public synchronized void useTunnelLock(Train train)
{
System.out.println(train + " " + train.getDirection());
System.out.println(train + " exiting lock");
try
{
// occupy tunnel lock for 5s
Thread.sleep(5000);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
// swap direction of tunnel lock
direction = (direction == ASCENDING)? DESCENDING : ASCENDING;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎使用 信号量是合理的。
This seems like it would be reasonable to use a semaphore.