Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可监控和调整的多个定时器_Java_Android_Multithreading_Timer - Fatal编程技术网

Java 可监控和调整的多个定时器

Java 可监控和调整的多个定时器,java,android,multithreading,timer,Java,Android,Multithreading,Timer,我需要一种方法来让计时器在start命令上运行。最初,计时器将被告知运行多长时间。然而,我需要一种方法来改变这中间的方式。如果用户指定要更改计时器运行的时间(通过同时减少、增加甚至结束计时器)。我还希望能够通过以分:秒格式显示当前时间来更新UI 用户可能还希望生成多个计时器-因此他们应该有一种方法来监视和访问他们设置的不同计时器 目前,这就是我所拥有的——据我所知,它是有效的。。但我对此感觉不太好:我怎样才能让它变得更好?或者正确的方法是什么?请记住,此计时器运行时会发生其他事情-例如,用户可能

我需要一种方法来让计时器在start命令上运行。最初,计时器将被告知运行多长时间。然而,我需要一种方法来改变这中间的方式。如果用户指定要更改计时器运行的时间(通过同时减少、增加甚至结束计时器)。我还希望能够通过以分:秒格式显示当前时间来更新UI

用户可能还希望生成多个计时器-因此他们应该有一种方法来监视和访问他们设置的不同计时器

目前,这就是我所拥有的——据我所知,它是有效的。。但我对此感觉不太好:我怎样才能让它变得更好?或者正确的方法是什么?请记住,此计时器运行时会发生其他事情-例如,用户可能正在与UI交互并执行与计时器无关的不同命令

我真的不太了解线程或线程。。。所以这对我来说是比较新的

提前谢谢

主菜单中:

//every time they request for a new timer, i will instantiate a new Task object..
Task task = new Task(); 
task.run()
task.getTaskTime(); 
Task.java:

public class Task extends Thread{
    @Override
    public void run()
    {
        timer = new Timer(60 * 60); 
        timer.start();
    }

    public boolean getTImerRunning() {
        return timer.timerRunning;
    }

    public double getTaskTime() {
        return timer.currentTime;
    }
}
public class Timer extends Thread {
    public static final int DEFAULT_TIMER = 180; 
    private long startMinute = 0;
    private int _timer; 
    public double currentTime = 0; 
    public boolean timerRunning = true; 

    Timer(int timer) {
        this._timer = timer; 
    }

    Timer() {
        this._timer = DEFAULT_TIMER; 
    }

    @Override
    public void run(){
         long start = System.currentTimeMillis();
         startMinute = start; 

         while(elapsedTime() <= this._timer) {
             currentTime = elapsedTime(); 
         }
         timerRunning = false; 
    }

    /**
     * Return elapsed time since this object was created.
     */
    private double elapsedTime() {
        long now = System.currentTimeMillis();
        return (now - startMinute);
    }
}
在Timer.java中:

public class Task extends Thread{
    @Override
    public void run()
    {
        timer = new Timer(60 * 60); 
        timer.start();
    }

    public boolean getTImerRunning() {
        return timer.timerRunning;
    }

    public double getTaskTime() {
        return timer.currentTime;
    }
}
public class Timer extends Thread {
    public static final int DEFAULT_TIMER = 180; 
    private long startMinute = 0;
    private int _timer; 
    public double currentTime = 0; 
    public boolean timerRunning = true; 

    Timer(int timer) {
        this._timer = timer; 
    }

    Timer() {
        this._timer = DEFAULT_TIMER; 
    }

    @Override
    public void run(){
         long start = System.currentTimeMillis();
         startMinute = start; 

         while(elapsedTime() <= this._timer) {
             currentTime = elapsedTime(); 
         }
         timerRunning = false; 
    }

    /**
     * Return elapsed time since this object was created.
     */
    private double elapsedTime() {
        long now = System.currentTimeMillis();
        return (now - startMinute);
    }
}
公共类计时器扩展线程{
公共静态最终整数默认定时器=180;
专用长启动分钟=0;
专用int_定时器;
公共双电流时间=0;
公共布尔timerRunning=true;
定时器(整数定时器){
这个。_timer=计时器;
}
计时器(){
这个。_定时器=默认的_定时器;
}
@凌驾
公开募捐{
长启动=System.currentTimeMillis();
开始分钟=开始;

虽然(elapsedTime()每次用户创建“计时器”,但不要创建新任务,而是创建一个对象MyTimer,该对象保存创建时间和倒计时持续时间,并将实例添加到ArrayList中

第一次也是第一次,用户创建一个计时器。您将创建一个每秒执行一次的任务。当该任务执行时,它将更新UI并循环遍历ArrayList,并通过调整倒计时持续时间来更新MyTimer的每个实例

这将使事情保持简单并减少开销

祝你好运