0

I have this thread class I am trying to work with.

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;
}
}

I am trying to get the variable timeToDisplay to change every milisecond that the thread runs. The thread is supposed to run for a set amount of et(execution time). All I need the task to do is run according to the execution time and assign the current time to timeToDisplay Variable.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
TerNovi
  • 390
  • 5
  • 16
  • What is it doing? Are there exceptions? – Robert H Nov 27 '13 at 19:07
  • So this code is the task (Runnable). What's running this runnable? – Cruncher Nov 27 '13 at 19:08
  • The task is not complicated all it has to do is run for the execution time. I was required to design a CPU scheduler that actually runs tasks. The tasks are not required to do anything except run. I have my CPU scheduler set up all I have to do is run the task for the amount of execution time. But i need the variable timeToDisplay to show when the task is running and for how long it is running. – TerNovi Nov 27 '13 at 19:11

3 Answers3

0

I am not sure that is what you expect, but:

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

You may need to synchronize your get and set methods, depending on what you are trying to achieve.

benjamin.d
  • 2,801
  • 3
  • 23
  • 35
0

Here is a sample of a simple scheduled job with comments. Feel free to ask for details.

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!");
    }
}
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
  • wow thank you, this is sorta what I needed, I just have to modify the code. What does the join commad do though? – TerNovi Nov 27 '13 at 20:13
  • Join will wait for a thread to finish. After you start a thread there are two execution line being run simultaneously - one is `main()` and the other is `Execution.run`. If you don't call `join()` in a `main()` thread, it will quit immediately without waiting for `Execution.run` to finish. The problem is that with `main()` finishing, the whole app will exit, so the thread `Execution.run` will quit in the very beginning. To prevent that `main()` waits for `Execution.run` to finish. – Andrey Chaschev Nov 27 '13 at 20:19
  • ok. I am now trying: Thread t1 = new Thread(new Execution(Name1,et1, new Runnable(){ @Override public void run() { p1RunningState.setText("Running"); } })); t1.start(); if(t1.isAlive()){ p1RunningState.setText("Running"); } t1.join(); p1RunningState is simply a jLabel. I want it to display running while the process is running. – TerNovi Nov 27 '13 at 20:42
  • But i seem to have the logic reversed. It will only change to whats inside the run statement after the task has finished. I don't fully understand the code may you can help @Andrey Chaschev – TerNovi Nov 27 '13 at 20:44
  • sorry i fixed it right under so you can see what i was trying to show you. – TerNovi Nov 27 '13 at 20:46
  • I'm afraid that the SO's editing system won't let you modify the answer. I don't quite understand how setting a label could not work for you. You just set it at the beginning and at the end of your run... You could post this as a different question, but be aware though that moderators might close it because of the rules - multiple questions for the same task are frowned upon. – Andrey Chaschev Nov 27 '13 at 21:08
  • For example, if the process is running it shows its stopped, but if it stopped then it shows it's running. – TerNovi Nov 27 '13 at 21:10
  • Ah, I see. You need to update it via `SwingUtilities.invokeLater`: http://stackoverflow.com/questions/7196889/swingutilities-invokelater. – Andrey Chaschev Nov 27 '13 at 21:15
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();
TerNovi
  • 390
  • 5
  • 16