Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中每隔10秒显示循环中的值_Java_Shell_Loops_Servlets_While Loop - Fatal编程技术网

如何在java中每隔10秒显示循环中的值

如何在java中每隔10秒显示循环中的值,java,shell,loops,servlets,while-loop,Java,Shell,Loops,Servlets,While Loop,我有一个while循环,其中通过附加shell脚本的输出来构造一个JAVA字符串 System.out.println("Here is the standard output of the command:\n"); while ((s = stdInput.readLine()) != null) { refreshString = refreshString + s; System.out.println(s); } 此shell脚本需要2小时才能执行。有没有一种方法可以让我每10秒抓取

我有一个while循环,其中通过附加shell脚本的输出来构造一个JAVA字符串

System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
refreshString = refreshString + s;
System.out.println(s);
}
此shell脚本需要2小时才能执行。有没有一种方法可以让我每10秒抓取一次字符串(当前字符串的快照)?我正在开发一个JAVA应用程序,在这个应用程序中,我需要每10秒在浏览器上显示一次该字符串。(我使用自动页面刷新库来实现这一点。页面会自动刷新。)

现在,每次刷新页面时,我的页面都必须显示当前的“refreshString”

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
PrintWriter out = response.getWriter();
...
out.println(refreshString);

如果我的问题不清楚,请告诉我。

您可以使用该文件。脚本将输出写入文件。然后,每当调用此文件时,servlet都会读取它。

那么。。。每十秒钟,它就会打印你的字符串,不需要太多的更改

long ini = System.currentTimeMillis();
while ((s = stdInput.readLine()) != null) {
    refreshString = refreshString + s;
    if(System.currentTimeMillis() - ini >= 10 * 1000){
        ini = System.currentTimeMillis();
        System.out.println(s);
    }
}
编辑:对不起,你的意思是每十秒钟! 希望有帮助

试试这个:

Thread thread = new Thread() {

    public void run() {
        while (true){
            try {
                Thread.sleep(10000);
                System.out.println(myString);
                myString = "";
            } catch (Exception e){}                
        }
    }
};

thread.start();        
for (int i=0; ; i++){
    myString += i;
    // Do other stuff
}

可能是我们的线程。sleep()?你是要我使用线程吗?如何在带有线程的while循环中获取字符串的快照?在循环中使用Thread.sleep(10000);每次循环运行时结束,暂停10秒。当您编写简单的“Hello world”时,您已经使用了一个线程。每n行输入而不是每10秒一行如何?当然不要使用
thread.sleep()
,因此您要求我暂停while循环执行并打印当前字符串值@disable1992System.out.println(s)在Tomcat控制台上打印字符串。我所需要的与之完全相似。我需要我的浏览器来显示tomcat控制台显示的内容。然后你必须将其存储在一个文件memcached中。。在任何地方因此,每次客户端请求时,您都可以读取存储的数据并将其打印到您的网页上,并且控制台上的输出非常巨大。它会发生剧烈的变化。然后不要使用文件,因为它可能会占用太多的磁盘空间。考虑一下控制台输出的大小。多少钱?1、10、1000Mb?也许你想把它存储在java堆中,也许它太大了,你需要使用任何外部帮助,因为这基本上是一种短时间存储数据的简单方法。你认为我可以将内容写入一个文件,然后同时从那里检索吗?脚本写入(附加)文件,当servlet读取文件时。因此,我没有看到任何潜在的问题。但是,我们需要尝试确定。