java - 如何在运行时打破无限循环?

标签 java eclipse

假设我有:

b = true;
while(b){}

在这之后还有什么办法可以继续吗?喜欢某种无需停止并重新运行程序即可修改 b 值的方法吗?

我想将其作为一种简单的方法来将程序暂停一段未指定的时间,该时间一直在变化。

最佳答案

您可以读取b的值来自用户在运行时可修改的数据源。

这可以是数据库、网络套接字或简单文件中的任何内容。

在这种情况下,我会推荐一个套接字。下面是一个非常粗糙但有效的示例。

在 Eclipse 中启动程序后,您需要打开一个终端窗口并 telnet localhost 10008 .

通过套接字接受的命令是:

<numeric value> = 将应用程序暂停该毫秒数

BYE = 关闭套接字

STOP = 完全停止应用程序

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class RuntimePause extends Thread {
    protected static boolean appRunning = true;
    protected Socket clientSocket;

    private long pause = 0;

    public static void main(String[] args) throws IOException {

        RuntimePause app = new RuntimePause();
        app.start();

        while (true) {
            app.listen();
        }

    }

    public void run() {

        while (appRunning) {

            System.out.println("App running...");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            if (pause > 0) {
                System.out.println("App pausing for " + pause + " ms");
                try {
                    Thread.sleep(pause);
                } catch (InterruptedException e) {
                }
                pause = 0;
            }

        }
    }

    public void listen() {

        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(10008);
            System.out.println("Connection Socket Created");
            try {
                while (appRunning) {
                    System.out.println("Waiting for Connection");
                    new NetworkSocket(serverSocket.accept());
                }
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.exit(1);
            }
        } catch (IOException e) {
            System.err.println("Could not listen on port: 10008.");
            System.exit(1);
        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                System.err.println("Could not close port: 10008.");
                System.exit(1);
            }
        }
    }

    public class NetworkSocket extends Thread {

        public NetworkSocket(Socket clientSoc) {
            clientSocket = clientSoc;
            start();
        }

        public void run() {
            {
                System.out.println("New Communication Thread Started");

                try {
                    PrintWriter out = new PrintWriter(
                            clientSocket.getOutputStream(), true);
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(clientSocket.getInputStream()));

                    String inputLine;

                    while ((inputLine = in.readLine()) != null) {
                        System.out.println("Received: " + inputLine);

                        // Client sent a pause command
                        try {
                            long pauseCommand = Long.parseLong(inputLine);
                            pause = pauseCommand;
                            out.println("OK, pausing for " + inputLine + " ms");
                        } catch (NumberFormatException e) {
                            //
                        }

                        // Client wishes to terminate connection to socket
                        if (inputLine.equals("BYE")) {
                            out.println("OK, bye!");
                            break;
                        }

                        // Client orders the app to stop
                        if (inputLine.equals("STOP")) {
                            out.println("OK, stopping!");
                            System.exit(1);
                        }
                    }

                    out.close();
                    in.close();
                    clientSocket.close();
                } catch (IOException e) {
                    System.err.println("Problem with Communication Server");
                    System.exit(1);
                }
            }
        }
    }
}

顺便说一句,此代码尚未准备好用于生产。它只是作为如何解决问题的示例。您希望确保以线程安全的方式访问变量。

关于java - 如何在运行时打破无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204203/

相关文章:

android - 将示例项目导入eclipse

java - 如何以编程方式列出正在运行的 Eclipse 实例中的所有功能

java - 将Java程序绑定(bind)到Cocoa接口(interface)

java - eclipse中java的匹配花括号之间的垂直线

Java EE @Schedule 和服务器关闭

Java EE 6 应用程序客户端登录

java - "Cannot refer to a non-final variable buttonflag inside an inner class defined in a different method"

java - while 循环中两个条件之间的差异

java - JNI GetObjectClass 始终返回 java/lang/Class

eclipse - Egit 在提交后上演?