如何让线程在某个阶段睡觉?

时间:2014-04-24 17:04:48

标签: java multithreading

@Override
public void run(int interfaceId, int componentId) {
    if (interfaceId == 382 && componentId == 19) {
        player.stopAll();
        player.lock(4);
        player.setNextAnimation(new Animation(6132));
        final WorldTile toTile = new WorldTile(ditch.getRotation() == 3 || ditch.getRotation() == 1 ? ditch.getX() - 1 : player.getX(),
                ditch.getRotation() == 0 || ditch.getRotation() == 2 ? ditch.getY() + 2 : player.getY(), ditch.getPlane());
        player.setNextForceMovement(new ForceMovement(
                new WorldTile(player), 1, toTile, 2,    ditch.getRotation() == 0 || ditch.getRotation() == 2 ? ForceMovement.NORTH : ForceMovement.WEST));
        try {
        Thread.sleep(3000);
        } catch(InterruptedException e){
            e.printStackTrace();
        }
        WorldTasksManager.schedule(new WorldTask() {
            @Override
            public void run() {
                player.setNextWorldTile(toTile);
                player.faceObject(ditch);
                player.getControlerManager().startControler("Wilderness");
                player.resetReceivedDamage();
            }
        }, 2);
    } else
        player.closeInterfaces();
    end();
}

你可以在代码中间看到我在" Thread.sleep(3000)中添加的内容;"因为我希望进程停止只持续3秒然后继续,但是当我尝试运行它时,线程休眠在其后面的任何其他代码之前执行,为什么如此,我将如何允许它以我想要的顺序执行?

喝彩!

1 个答案:

答案 0 :(得分:0)

试试这个(你可能需要移动catch块):

    @Override
 public void run(int interfaceId, int componentId) {

 if (interfaceId == 382 && componentId == 19) {
    System.out.println("Am i printing?????");
    try {
    Thread.sleep(3000);
    } catch(InterruptedException e){
        e.printStackTrace();
    }
    WorldTasksManager.schedule(new WorldTask() {
        @Override
        public void run() {
            player.setNextWorldTile(toTile);
            player.faceObject(ditch);
            player.getControlerManager().startControler("Wilderness");
            player.resetReceivedDamage();
        }
    }, 2);

} else
    player.closeInterfaces();
end();

}