javafx: How to stop an object's background task running when the object itself is deleted from Observable List?

mynameisJEFF

I have a Trade object and whenever it is constructed, it kicks off a task in the background (i.e. say download currentPrice) and start running every 10 seconds.

When the Trade object is created, it is also added to a ObservableList .

Problem: When I delete the object from the ObservableList, I can see that the task (download currentPrice) is still running in the background, although the object is not in the ObservableList anymore.

  1. Is the object actually deleted ?
  2. How can I stop the object's background task from running when the object itself is already removed from the ObservableList?

My goal is to delete the object forever and stop its background task from running as well.

public class Trade{

....

private final ScheduledService<Number> priceService = new ScheduledService<Number>() {
        @Override
        public Task<Number> createTask(){
            return new Task<Number>() {
                @Override
                public Number call() throws InterruptedException, IOException {
                    return getCurrentPriceFromGoogleFinance();
                }
            };
        }
    };

    public Trade(){
        priceService.setPeriod(Duration.seconds(10));
        priceService.setOnFailed(e -> priceService.getException().printStackTrace());
        this.currentPrice = new ReadOnlyDoubleWrapper(0);
        this.currentPrice.bind(priceService.lastValueProperty());
        startMonitoring();
    }


     public ReadOnlyDoubleProperty currentPriceProperty(){
         return this.currentPrice.getReadOnlyProperty();
     }

     public final double getCurrentPrice(){
         return currentPriceProperty().get();
     }

     // multi-threading
     public final void startMonitoring() {
         priceService.restart();
     }

     public final void stopMonitoring() {
         priceService.cancel();
     }

     ....
}
Manik

If I understand garbage collector right, your object will live for some time before it gets collected even after you remove it from your list.

To stop the task, add ListChangeListener to your ObservableList. You can get removed Trade and call stopMonitoring() on it. That could hopefuly stop the task.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

stop task from running when file is open

From Dev

How to make a object delete itself from a list.

From Dev

How to stop Cortana from running in the background?

From Dev

How does a standalone object get affected when the actual object is deleted from realm?

From Dev

How to Stop Reading From a BufferedReader when sending an Object in Java?

From Dev

Disposing an object with a running task?

From Dev

JavaFx removing from pane object when I am in this object's class

From Dev

JavaFx removing from pane object when I am in this object's class

From Dev

Raising an event from an observable object to it's container

From Dev

How to stop a background process when the foreground process finished completing the task

From Dev

Why is ActiveRecord trying to delete the row corresponding to an object when it's deleted from an array?

From Dev

How the object refers to itself?

From Dev

How to return an object that was deleted?

From Dev

Serialize object when the object inherits from list

From Dev

How to have a Game Object remove itself from a list? (Unity3d)

From Dev

how to stop a running async task?

From Dev

RxJava: How to extract object from observable?

From Dev

RxJava: How to extract object from observable?

From Dev

How to unsubscribe from Observable if there is no object of type "Subscription"?

From Dev

How to stop an object from moving past a point

From Dev

Update NSFetchedResultsController object when relationship object was deleted

From Dev

How to get the source of a javascript bookmark from itself when running?

From Dev

How to get the source of a javascript bookmark from itself when running?

From Dev

Last object in a list isn't deleted after deleting it. When is it effectively deleted?

From Dev

Python change object reference from object itself

From Dev

How to extract single property from Python object when it is in a list?

From Dev

how to get or set object's attribute from a list in maya python?

From Dev

Java 8: How to create map from list where Key is fetched from same class (empID) and value as object(Employee) itself?

From Dev

Get object from Task<>

Related Related

  1. 1

    stop task from running when file is open

  2. 2

    How to make a object delete itself from a list.

  3. 3

    How to stop Cortana from running in the background?

  4. 4

    How does a standalone object get affected when the actual object is deleted from realm?

  5. 5

    How to Stop Reading From a BufferedReader when sending an Object in Java?

  6. 6

    Disposing an object with a running task?

  7. 7

    JavaFx removing from pane object when I am in this object's class

  8. 8

    JavaFx removing from pane object when I am in this object's class

  9. 9

    Raising an event from an observable object to it's container

  10. 10

    How to stop a background process when the foreground process finished completing the task

  11. 11

    Why is ActiveRecord trying to delete the row corresponding to an object when it's deleted from an array?

  12. 12

    How the object refers to itself?

  13. 13

    How to return an object that was deleted?

  14. 14

    Serialize object when the object inherits from list

  15. 15

    How to have a Game Object remove itself from a list? (Unity3d)

  16. 16

    how to stop a running async task?

  17. 17

    RxJava: How to extract object from observable?

  18. 18

    RxJava: How to extract object from observable?

  19. 19

    How to unsubscribe from Observable if there is no object of type "Subscription"?

  20. 20

    How to stop an object from moving past a point

  21. 21

    Update NSFetchedResultsController object when relationship object was deleted

  22. 22

    How to get the source of a javascript bookmark from itself when running?

  23. 23

    How to get the source of a javascript bookmark from itself when running?

  24. 24

    Last object in a list isn't deleted after deleting it. When is it effectively deleted?

  25. 25

    Python change object reference from object itself

  26. 26

    How to extract single property from Python object when it is in a list?

  27. 27

    how to get or set object's attribute from a list in maya python?

  28. 28

    Java 8: How to create map from list where Key is fetched from same class (empID) and value as object(Employee) itself?

  29. 29

    Get object from Task<>

HotTag

Archive