1

This may be a poorly worded title, but I am trying to gather data and statistics on my application from the very start of it. I am using JavaFX and I have a model class that will be dealing with all of the data and processing.

What I want is for that model class to start gathering the data (like runtime, thread count, mem usage...) from the moment the app starts. But I also want it to keep updating these values every second, which means I need it to run on some sort of ScheduledExecutorThread or something.

How can I do so that from the very start of this program, the model class will run the "update()" function every second?

G Boggs
  • 381
  • 1
  • 6
  • 19
  • @G Boggs Take a look at [this](http://stackoverflow.com/questions/16128423/how-to-update-the-label-box-every-2-seconds-in-java-fx/16138351#16138351). – Chris Oct 01 '14 at 19:40
  • @Chris I'm not entirely sure this is what I want to do. I want the class to start gathering data from the start. First off, how can I do that. Secondly, I dont want to have to call a function, I want it to start the thread on its own essentially. – G Boggs Oct 01 '14 at 19:44

1 Answers1

1

Any time you are looking to do fixed-interval opetations in Java you should be investigating the ScheduledExecutorService. In your case, something like:

private static final ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(1,
    new ThreadFactory() {
        public Thread newThread(Runnable r) {
            Thread t = new Thread("Tick Thread", r);
            t.setDaemon(true);
            return t;
        }
    });

private static final AtomicReference<Controller> CONTROLLER = new AtomicReference<>();

public static final void setController(Controller c) {
    CONTROLLER.compareAndSet(null, c);
}

static {

    Runnable task = new Runnable() {
        public void run() {
            //do something each second....
            // call the 'update()' method:
            Controller c = CONTROLLER.get();
            if (c != null) {
                c.update();
            }
        }
    }

    // delay 1 second, repeat each second.
    service.scheduleAtFixedRate(task, 1, 1, TimeUnit.SECONDS);
}

You can take that, and put it in a Static initializer of the class, and you will get the system started the moment the class is loaded.... You will likely want the thread on the ExecutorService to be a Daemon thead... so you need a custom thread factory.....

The above code will start immediately, and will update the Controller, if there is one, every second.

The moment you create c controller, you can set the value using the static method.

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Ok thats what I was thinking. But my issue is that I have a model class that is the where all of the data acquisition happens. If I want this class to start gathering data from the moment the program starts how do I do so? And also how can I get the SeS to start on its own? – G Boggs Oct 01 '14 at 19:47
  • 1
    Let me update a bit with more detail.... is the 'update()' method a static method? – rolfl Oct 01 '14 at 19:49
  • It its not, it is part of a controller class that the model will hopefully have access to – G Boggs Oct 01 '14 at 19:51
  • Updated to be static, and thread safe. Call the setController when you have one available, and the ticker will just pick it up and go. – rolfl Oct 01 '14 at 19:55