2012-02-29 6 views
1

이렇게 ...Java TimerTask, 취소시 호출되지 않습니다.

플러그인을 만들고 있습니다.

내가 기본

기초에서 세계적으로

내가 만들라는 메인 클래스가 있습니다

또한
static Timer enterdungeon = new Timer(); 
static Timer finddungeon = new Timer(); 
static Timer lootdungeon = new Timer(); 

I라는 클래스 작업

enterdungeon 타이머가 시간의 고정 기간이 있고 사용하면 예상대로 작동하는 것 같습니다. thelootdungeon timer도 마찬가지입니다.

finddungeon 타이머는 기본 이벤트가 트리거되면 중단 될 수 있습니다.

이벤트가 발생합니다. 이 이벤트의 맨 위 줄은 다음과 같습니다. finddungeon.cancel();

lootdungeon 타이머를 시작한 후.

문제는 아래가 계속 실행의 finddungeon 타이머가 취소되지 않습니다되는 작업 클래스입니다 : 여기

import java.util.TimerTask; 
import me.boduzapho.Basics.DoWarp.Returner; 
import org.bukkit.entity.Player; 

public class task extends TimerTask 
{ 
private final Player _player; 
private final int ticks; 
private int cnt = 0; 
private final int _sec; 
private final String _message; 

public task(Player player, int sec, String message) 
{ 
    this._player = player; 
    this._sec = sec; 
    this._message = message; 
    this.ticks = sec; 
} 

private void timetoloot(Player p) 
{ 

    p.sendMessage("SUCCESS! Nice Job, Enjoy the loot!"); 
    Returner loc1 = DoWarp.getwarp("launch", Basics.warps, Basics.wx,Basics.wy, Basics.wz, p); 
    DoWarp.warpme(loc1.x, loc1.y, loc1.z, p, false, Basics.plugin); 

} 

private void failedwhiteblock(Player p) 
{ 
    p.sendMessage("FAIL! You did not find the white block. Sending you back. TRY AGAIN!"); 
    Returner loc1 = DoWarp.getwarp("launch", Basics.warps, Basics.wx, Basics.wy, Basics.wz, p); 
    DoWarp.warpme(loc1.x, loc1.y, loc1.z, p, false, Basics.plugin); 

} 

private void enterdungeon(Player p) 
{ 
    Basics.Stage.setLine(3, "Off you Go!"); 
    Basics.Stage.update(); 
    Basics.Stage.setLine(0, ""); 
    Basics.Stage.setLine(1, ""); 
    Basics.Stage.setLine(2, ""); 
    Basics.Stage.setLine(3, ""); 
    Basics.Stage.update(); 

    Basics.cDoClear(p); 
    Basics.cDoSpawners(p); 
    Basics.cDoRed(p); 
    Returner loc1 = DoWarp.getwarp("dstart", Basics.warps, Basics.wx, Basics.wy, Basics.wz, p); 
    DoWarp.warpme(loc1.x, loc1.y, loc1.z, p, false, Basics.plugin); 
    Basics.DungeonPlayer = p; 
    p.sendMessage("Welcome to the Dungeon, you have 1 minuite to locate and click the white block."); 
    p.sendMessage("If you fail you will be returned to spawn. If you find it the treasures will be revieled"); 
    p.sendMessage("and the monsters banished for 1 min so you can loot the chests! After which you will"); 
    p.sendMessage("Be warped back to spawn with your Loot!"); 
    Basics.finddungeon.schedule(new task(_player, 30, "Time left to find the WHITE block :"), 0, 1000); 
    Basics.enterdungeon.cancel(); 
} 

@Override 
public void run() 
{ 
    while (cnt < ticks) 
    { 
     try 
     { 
      Thread.sleep(1 * 1000); 
      _player.sendMessage(_message + " " + Integer.toString(_sec - cnt)); 
      ++cnt; 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    _player.sendMessage("Done!"); 
    if (_message == "Time left:") 
    { 
     enterdungeon(_player); 
    } 

    if (_message == "Time left to find the WHITE block :") 
    { 
     failedwhiteblock(_player); 
    } 

    if (_message == "Time left to LOOT:") 
    { 
     timetoloot(_player); 
    } 

    // 

    return; 

} 
} 

가 finddungeon을 취소하도록되어 기본 (기본 클래스)에서 호출하는 기능입니다 시간제 노동자.

// white block in dungeon 
     if (DungeonPlayer == player) 
     { 
      if ((block != null) && (block.getType() == Material.WOOL)) 
      { 
       player.sendMessage("Canceling finddungeon from Basics"); 
       finddungeon.cancel(); 
       cDoClear(player); 
       cDoChests(player); 

       player.sendMessage("Congradulations! Time to Loot your rewards for finding the White Block!"); 
       Timer lootdungeon = new Timer(); 
       lootdungeon.schedule(new task(player, 10, "Time left to LOOT:"), 0, 1000); 

       return; 
       // *** 
      } 
     } 

누구든지이 문제를 해결할 수 있습니까?

+0

타이머의 실행 이벤트 내에서 확인되는 부울 변수를 전역 적으로 선언하려고했습니다. 기본적으로 이것을 false로 설정하고,이 작업을 계속할 시간을 지시합니다. 화이트 블록 이벤트가 트리거 될 때 run 이벤트에 cancel을 호출하도록 지시하기 위해 이것을 true로 설정한다. (자바의 지시에 따라, 타이머를 죽이는 어리석은 방법). 이것은 또한 작동하지 않습니다. 반영된 "chat"을 통해 타이머 부울 변수가 false에서 true로 변경되고 cancel(); 호출되고 있지만 타이머가 계속됩니다. – Boduzapho

답변

1

원인 TimerTask.cancel이 활성 작업에 아무 것도하지 않고 단순히 스케줄러를 지 웁니다. cancel 메서드를 재정의하거나이 메서드를 시작점으로 사용해야합니다.

class MyTimerTask extends TimerTask { 
    private volatile Thread thread; 

    @Override 
    public void run() { 
     thread = Thread.currentThread(); 
     //do your task and keep your eye on InterruptedException when doing Sleeps, Waits 
     //also check Thread.interrupted() 
    } 

    public boolean cancel() { 
     Thread thread = this.thread; 
     if (thread != null) { 
      thread.interrupt(); 
     } 
     return super.cancel(); 
    } 
} 
관련 문제