Java解哲学家就餐问题
来源:优易学  2011-9-12 12:11:05   【优易学:中国教育考试门户网】   资料下载   IT书店

  public boolean chopsticksFree()
  {
    return !host.getChopstick(index) &&
    !host.getChopstick((index+1)%host.getCount());
  }
  public void run()
  {
    while(true)
    {
    grabChopsticks();
    eat();
    think();
    }
  }
  private synchronized void grabChopsticks() /**临界区函数,确保哲学家在没有筷子或筷子不够时思考,满足条件后才就餐*/
  {
    while( !chopsticksFree())
    {
    try
    {
   wait();
    }
    catch( InterruptedException e){}
    }
    takeChopsticks();
    notifyAll();
  }
  private void takeChopsticks()
  {
    setLeftChopstick( true );
    setRightChopstick( true );
    setEat(true);
    host.dumpStatus();
  }

上一页  [1] [2] [3] [4] [5] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训