I am writing a graphic editor on javaFX.
The program window will be drawn in the main controller, and the image will be drawn in the background thread. The drawing stream waits for the command and starts drawing. after which he enters the next round and waits for the team again. I do not understand how with this approach you can close the background thread (when closing the main program window). I would be grateful for any advice. Draftsman’s stream code:
public class DrawService implements Runnable{ private GraphicsContext gc; private Shape shape; private DrawService() { } private DrawService(MainController mc) { Thread thread = new Thread(this); this.isAlive = true; this.gc = mc.getCnvs().getGraphicsContext2D(); this.gcWidth = mc.getCnvs().getWidth(); this.gcHeight = mc.getCnvs().getHeight(); thread.start(); } public static DrawService getInstance(MainController mc){ if (instance == null){ synchronized (DrawService.class){ if (instance == null) instance = new DrawService(mc); } } return instance; } @Override public void run() { while (isAlive) { synchronized (this) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } drawShape(shape); if (Thread.interrupted()) { isAlive = false; return; } } } } public void draw(Shape shape) { synchronized (this){ this.shape = shape; notify(); } } I do not know how to close this thread by closing the main window. Tell me please.