Dispose JavaFX Tasks -
i have static borderpane contextmenu insight task
task task = new task() { @override protected void call() throws exception { platform.runlater(new runnable() { @override public void run() { try { contextmenu = new contextmenu(); menuitem item1 = new menuitem("about"); item1.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { system.out.println("about"); } }); menuitem item2 = new menuitem("preferences"); item2.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { system.out.println("preferences"); } }); menuitem item3 = new menuitem("close"); item3.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { } }); contextmenu.getitems().addall(item1, item2, item3); bp.setoncontextmenurequested(new eventhandler<contextmenuevent>() { @override public void handle(contextmenuevent event) { contextmenu.show(bp, event.getscreenx(), event.getscreeny()); event.consume(); } }); bp.addeventhandler(mouseevent.mouse_pressed, new eventhandler<mouseevent>() { @override public void handle(mouseevent event) { contextmenu.hide(); } }); } catch (exception ex) { ex.printstacktrace(); } { } } }); return null; } }; new thread(task).start();
i noticed when close component holds borderpane java threads not disposed still initialized memory. i'm not sure caused static borderpane. after task completed java thread should disposed. idea why happening?
the problem not task
, anonymous classes in runnable
.
in next piece of code:
bp.setoncontextmenurequested(new eventhandler<contextmenuevent>() { @override public void handle(contextmenuevent event) { //... } });
you introduce anonymous class extending eventhandler
holds inner link runnable
. solve can use nested static class instead.
p.s.: unfortunately can't make anonymous class static in java, see is possible make anonymous inner classes in java static?
Comments
Post a Comment