具有时序实现的Java线程

时间:2013-11-27 19:04:41

标签: java multithreading time

我正在尝试使用此线程类。我之前从未使用过Java线程。

public class Execution implements Runnable {
public String name;
public double time;
public double timeToDisplay;

public Execution(String name, double et){
   this.name = name;
   this.time = (et*1000);
}
public void run(){
try{


}catch(Exception e){}
}

/**
 * @return the timeToDisplay
 */
public double getTimeToDisplay() {
    return timeToDisplay;
}

/**
 * @param timeToDisplay the timeToDisplay to set
 */
public void setTimeToDisplay(double timeToDisplay) {
    this.timeToDisplay = timeToDisplay;
}
}

我试图让变量timeToDisplay改变线程运行的每一个毫秒。该线程应该运行一定量的et(执行时间)。我从来没有使用过线程,而且我从来没有在java中处理过时间,我也不熟悉java,所以请在任何建议上尽可能准确和彻底。 我需要完成的任务是根据执行时间运行并将当前时间分配给timeToDisplay Variable。

感谢。

3 个答案:

答案 0 :(得分:0)

我不确定这是你的期望,但是:

public void run() {
    try { 
        while(true) {
           timeToDisplay++;
           Thread.sleep(1);
       }
    } catch (Exception e) {
    }
  }

您可能需要同步get和set方法,具体取决于您要实现的目标。

答案 1 :(得分:0)

以下是带注释的简单预定作业示例。随意询问详情。

public class Execution implements Runnable {
    public String name;

    protected long startedAtMs;

    // total timeout in ms
    protected long timeoutMs;


    // rate: 1 execution per 2 ms
    private long rateMs = 2;

    // when was the previousExecution
    private long prevExecutionMs;

    // action to run each 1 ms
    protected Runnable action;

    public Execution(String name, double et, Runnable action) {
        this.name = name;
        this.action = action;
        this.timeoutMs = (long) (et * 1000);

    }

    public void run() {
        startedAtMs = System.currentTimeMillis();
        prevExecutionMs = startedAtMs;

        while (true) {
            // check if the job was interrupted
            if (Thread.interrupted()) {
                return;
            }

            long now = System.currentTimeMillis();

            // check if it's time to finish
            if (now - startedAtMs > timeoutMs) {
                break;
            }

            // check if it's time to run the action
            if(now - prevExecutionMs > rateMs){
                // run the action
                action.run();
                // update the executed time
                prevExecutionMs = now;
            }
        }

    }

    // this getter could be used to get the running time
    public double getTimeToDisplay() {
        return (System.currentTimeMillis() - startedAtMs) / 1000D;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Execution("exec", 0.5, new Runnable() {
            @Override
            public void run() {
                System.out.println(new Date());
            }
        }));

        //starts the thread
        thread.start();

        //waits to finish
        thread.join();

        System.out.println("Done!");
    }
}

答案 2 :(得分:0)

Thread t1 = new Thread(new Execution(Name1,et1, new Runnable(){
                @Override
                public void run() {
                    p1RunningState.setText("Running");
                }
                 }));
                t1.start();

                if(!(t1.isAlive())){
                    p1RunningState.setText("Stopped");
                }
                t1.join();