英文:
Application start and stop functions with multithreading support
问题
我有一个应用程序,应该能够实现以下功能:
启动:
- 仅通过点击“开始”按钮
停止:
- 点击“停止”按钮
- 点击“X”按钮
- 当所有作业完成时(基本上是从非JavaFX线程调用的Application.stop()方法)
所有的操作应该都要“优雅”地发生。
按钮代码:
@FXML
private void start() {
    Application.start();
}
@FXML
private void stop() {
    Application.stop();
}
主要的应用程序start()和stop()函数(整个Application类):
public class Application {
    private static final AtomicBoolean isStarted = new AtomicBoolean(false),
            startPending = new AtomicBoolean(false),
            stopPending = new AtomicBoolean(false);
    private static final Object lock = new Object();
    //------以下是功能函数
    public static synchronized void start() {
        if(isStarted.get()) {
            Gui.showError("Application is already started");
            return;
        } else if(startPending.get() || stopPending.get()) return;
        isStarted.set(true);
        startPending.set(true);
        new Thread(startInternals()).start();
    }
    private static Runnable startInternals() {
        return () -> {
            synchronized(lock) {
                
				//做一些事情
                Logger.print("Started");
                startPending.set(false);
            }
        };
    }
    public static synchronized void stop() {
        if(!isStarted.get()) {
            Gui.showError("Application is not started");
            return;
        } else if(stopPending.get()) return;
        isStarted.set(false);
        stopPending.set(true);
        new Thread(stopInternals()).start();
    }
    private static Runnable stopInternals() {
        return () -> {
            synchronized(lock) {
                
				//做一些事情
				
                Logger.print("Stopped");
                stopPending.set(false);
            }
        };
    }
	
    //------以下是备注
    /*
    ===规则===
    --启动:
    已启动 -》 否
    已停止 -》 是
    正在启动 -》 否
    正在停止 -》 否
    --停止:
    已启动 -》 是
    已停止 -》 否
    正在启动 -》 是
    正在停止 -》 否
    */
}
这种方法是否完全正确?是否有更好/更简便的方法可以实现?
PS. 我已经添加了在启动时通过点击“X”按钮停止的能力(应用程序应该优雅地终止)。请查看文件底部的备注部分。
英文:
I have an application that should be able to:
Start:
- only by clicking "Start" button
Stop:
- clicking "Stop" button
- clicking "X" button
- when all jobs are done (basically Application.stop() method invoked from non-javafx thread)
All of the thing should happen "gracefully".
Buttons code:
@FXML
private void start() {
    Application.start();
}
@FXML
private void stop() {
    Application.stop();
}
And the main Application start() and stop() functions (whole Application class):
public class Application {
    private static final AtomicBoolean isStarted = new AtomicBoolean(false),
            startPending = new AtomicBoolean(false),
            stopPending = new AtomicBoolean(false);
    private static final Object lock = new Object();
    //------Functionals below
    public static synchronized void start() {
        if(isStarted.get()) {
            Gui.showError("Application is already started");
            return;
        } else if(startPending.get() || stopPending.get()) return;
        isStarted.set(true);
        startPending.set(true);
        new Thread(startInternals()).start();
    }
    private static Runnable startInternals() {
        return () -> {
            synchronized(lock) {
                
				//do something
                Logger.print("Started");
                startPending.set(false);
            }
        };
    }
    public static synchronized void stop() {
        if(!isStarted.get()) {
            Gui.showError("Application is not started");
            return;
        } else if(stopPending.get()) return;
        isStarted.set(false);
        stopPending.set(true);
        new Thread(stopInternals()).start();
    }
    private static Runnable stopInternals() {
        return () -> {
            synchronized(lock) {
                
				//do something
				
                Logger.print("Stopped");
                stopPending.set(false);
            }
        };
    }
	
    //------Notes below
    /*
    ===RULES===
    --START:
    STARTED -> NO
    STOPPED -> YES
    STARTING -> NO
    STOPPING -> NO
    --STOP:
    STARTED -> YES
    STOPPED -> NO
    STARTING -> YES
    STOPPING -> NO
    */
}
Is this approach fully correct? Is there any way I could do it better/easier?
PS. I've added "stop" ability when starting in case of "X" clicking (the app should terminate gracefully". Take a look at the bottom of the file for notes.
专注分享java语言的经验与见解,让所有开发者获益!



评论