每1秒做一次这段代码(Java)

时间:2016-07-25 04:20:10

标签: java timer minecraft forge

我有以下代码,但我想让它每秒运行一次,所以如果questNumber发生变化,那么currentQuestStats也会改变。 代码:http://pastebin.com/jTAwztPX

4 个答案:

答案 0 :(得分:3)

使用在JAVA 5中添加的ScheduledExecutorService,您必须导入以下类。

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

你可以像下面这样使用它:

ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        // TODO add your code here:
    }
}, 0, 1, TimeUnit.SECONDS);

<强>答案

  • checkQuestNumber()类中创建一个名为QuestsManagement的方法。
  • 还为QuestsManagement
  • 创建构造函数
  • 使用此值(currentQuestStats)初始化§e§lCurrent quest: Kill 1 zombie
  • currentQuestStatsquestNumber设为私有并对其进行封装。

这些更改后的QuestsManagement

public class QuestsManagement {

    private String currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
    private int questNumber = 1;

    public String getCurrentQuestStats() {
        return currentQuestStats;
    }

    public int getQuestNumber() {
        return questNumber;
    }

    QuestsManagement() {
        ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
        exec.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
               checkQuestNumber();
            }
        }, 0, 1, TimeUnit.SECONDS);
    }

    void checkQuestNumber() {

        if (questNumber == 1) {
            currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
            // change manual!
            questNumber = 2;
        } else if (questNumber == 2) {
            currentQuestStats = "§e§lCurrent quest: Walk 100 blocks";
            // change manual!
            questNumber = 1;
        }
    }
}

测试代码!

在自定义命令文件

创建QuestsManagement的实例并使用getCurrentQuestStats()方法

QuestsManagement m = new QuestsManagement();
player.addChatMessage("Test" + QuestsManagement.getCurrentQuestStats());

答案 1 :(得分:1)

使用多线程进行。这是代码:

public class QuestsManagement implements Runnable{
    public String currentQuestStats = "";
    public int questNumber = 1;
    Thread t;

    @Override
    public void run() {
        try {
            if(questNumber == 1) {
                currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
                if(questNumber == 2) {
                    currentQuestStats = "§e§lCurrent quest: Walk 100 blocks";
                }
            }
            Thread.sleep(1000);
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        Thread t = new Thread(new QuestsManagement());

        t.start();

        //do something else here... whatever you want to do
    }
}

答案 2 :(得分:1)

首先,您需要将要运行的所有代码添加到方法中。我假设您将其称为update()

您要做的是在主程序循环中添加以下内容:

long nextTick = currentTimeMillis() + 1000 ;//Set time of first game tick

QuestsManagement quest = new QuestsManagement();

//Main Loop
while (true) {
    /*
    Do some other stuff
    */

    if (System. currentTimeMillis() >= nextTick) {//Check to see if it is time or past time to update
        nextTick += 1000//Set next tick to one second in the future
        quest.update();
    }
}

请注意,您缺少update()方法,构造函数以及任何getter和setter。我也不会公开您的变量。

答案 3 :(得分:0)

嗯,这个问题可能有多个答案。 Java提供了各种执行时间操作的方法。一种方法是使用Timer类。

public class QuestsManagement {

public String currentQuestStats = "";
public int questNumber = 1;
static Timer t;
TimerTask task;
public static void main(String[] args) {
t = new Timer();
task = new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub

            if(questNumber == 1) {
                currentQuestStats = "§e§lCurrent quest: Kill 1 zombie";
            if(questNumber == 2) {
                 currentQuestStats = "§e§lCurrent quest: Walk 100 blocks";

         }
 } ;

t.scheduleAtFixedRate(task , 1000, 1000 );
}}

导入

import java.util.Timer;

import java.util.TimerTask;