java - 总是同一个线程获得 CPU 时间

标签 java multithreading synchronization

代码太大,所以我只复制有问题的部分。

这是一个类中的 run() 方法:

public void run(){
try{
    sleep(1000);
    while(true){
          synchronized(space){

        if(end)
          return;

        if(space[X][Y] == null)
          break;

        if(((Ship)space[X][Y]).isDestroyed){
          destroy();
          break;
        }

        if(isThereAnyShipsInTheArea() != 0){
          if(team != ((Ship)space[X][Y + isThereAnyShipsInTheArea()]).team){
            fight(isThereAnyShipsInTheArea());
          }
        }
        else
          move();

        if(isDestroyed){
          destroy();
          break;
        }
    }
    }
}
catch(InterruptedException ie){
  System.out.println("Interrupted exception!");
}

}

这是《星际迷航》的模拟。变量 team 代表该船属于哪个团队。如果船只在战斗中被摧毁或在移动时坠毁,则变量 isDestroyed 为 true。 isThereAnyShipsInTheArea() - 如果距离一或二,则船舶在范围内。 空间是维度为 [90]x[90] 的矩阵。

我认为问题出在 run 方法上,但我会给你一些其他方法。

    private int isThereAnyShipsInTheArea(){
  if(space[X][Y - 2] instanceof Ship && ((Ship)space[X][Y - 2]).isDestroyed == false)
    return -2;

  if(space[X][Y - 1] instanceof Ship && ((Ship)space[X][Y - 1]).isDestroyed == false)
    return -1;

  if(space[X][Y + 1] instanceof Ship && ((Ship)space[X][Y + 1]).isDestroyed == false)
    return 1;

  if(space[X][Y + 2] instanceof Ship && ((Ship)space[X][Y + 2]).isDestroyed == false)
    return 2;

  return 0;

}

 private synchronized void fight(int meet){


  while(((Ship)svemir[X][Y]).isDestroyed == false && ((Ship)space[X][Y + meet]).isDestroyed == false){
    if(((Ship)space[X][Y]).getProjectile() != 0){
      ((Ship)space[X][Y + meet]).setShield(((Ship)space[X][Y + meet]).getShield() - 1);
      ((Ship)space[X][Y + meet]).setWarp(((Ship)space[X][Y + meet]).getWarp() - 1);
      ((Ship)space[X][Y]).setProjectile(((Ship)space[X][Y]).getProjectile() - 1);

      if(((Ship)space[X][Y + meet]).getShield() == 0 || ((Ship)space[X][Y + meet]).getWarp() == 0){
        ((Ship)space[X][Y + meet]).isDestroyed = true;
        return;
      }
    }

    if(((Ship)space[X][Y + meet]).getProjectile() != 0){
      ((Ship)space[X][Y]).setShield(((Ship)space[X][Y]).getShield() - 1);
      ((Ship)space[X][Y]).setWarp(((Ship)space[X][Y]).getWarp() - 1);
      ((Ship)space[X][Y + meet]).setProjectile(((Ship)space[X][Y + meet]).getProjectile() - 1);

      if(((Ship)space[X][Y]).getShield() == 0 || ((Ship)space[X][Y]).getWarp() == 0){
        this.isDestroyed = true;
        return;
      }

    }

    if(((Ship)space[X][Y]).getProjectile() == 0 && ((Ship)space[X][Y + meet]).getProjectile() == 0)
      return;

  }

}

最佳答案

对我来说,你不应该执行 Thread.sleep() 因为它不会释放它获取的任何资源。使用 ScheduledExecutorService 安排任务或在对象监视器上执行 wait() 和 Yield()。

关于java - 总是同一个线程获得 CPU 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371554/

相关文章:

c# - Thread.Abort() 损坏是否已本地化?

c++ - 访问类私有(private)的大量数据的正确方法是什么?

c - 如何使用互斥量来同步此序列: (A or B)C (A or B) (A or B)C

java - Hibernate 不会加载一对多的关系集,即使使用 eager fetch

java - 获取jtable的单元格值

java - 有没有一种方法可以让多个线程添加到同一个列表对象?

java - 静态同步方法上的锁定是否会影响其实例的非静态同步方法?

java - 隐蔽同步

C++如何在银行账户转账时避免竞争条件

java eclipse 插件 : menuitems appear in windows but not on linux