如何设置此线程而不是从0开始?

时间:2015-08-17 02:23:24

标签: java multithreading

我有一个作为时钟工作的课程,如下所示:

public class Playtime extends Thread {

public int playtimei = 0;
@Override
public void run() {
    while(true)
    {
        playtimei++;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}

,在我的程序的新游戏中,这样开始:

Data.playtime = new Playtime();
Data.playtime.start();

导致它从零开始;但是,游戏需要能够从保存文件中读取int并从该数字开始。

如何使用特定的int启动它?

1 个答案:

答案 0 :(得分:1)

我认为只需简单地阅读您的配置时间,并在您的类中添加构造函数到Playtime。

public class Playtime extends Thread {

    int playtimei = 0;

    public Playtime(int playtimei) {
        this.playtimei = playtimei;
    }
    @Override
    public void run() {
        while(true)
        {
            playtimei++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

并称之为:

int time = loadFromFile();
Playtime playtime = new Playtime(time);