甚至可以让这个循环每次等待几秒钟?

时间:2014-05-28 15:27:31

标签: java jsp sleep

首先,是的,我是从网络浏览器调用它。这是一段相当长的代码,但我尽可能地缩短它。

基本上,我需要等待循环中每次迭代的1秒钟。几乎尝试了所有东西(.sleep()等),但它似乎并没有暂停。我需要这样做的原因是因为SimpleSocketClient正在调用一个每秒允许的限制为低的套接字。

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        String forwardToJsp = null;
                HttpSession session = request.getSession();
                String allUrls = request.getParameter("domains");
                ArrayList domainList = new ArrayList<String>();
                Scanner sc = new Scanner(allUrls);
                while (sc.hasNextLine()) {
                    String line = sc.nextLine();
                    domainList.add(line);

                    // process the line
                }
                sc.close();
                String pageHtml = null;
                String domain = "";
                String status = "";
                String registrant = "";
                String dates = "";
                String tag = "";
                String email = "";
                ArrayList domains = new ArrayList<Domain>();
                Domain theDomain;

                String ipAddress = request.getHeader("X-FORWARDED-FOR");
                if (ipAddress == null) {
                    ipAddress = request.getRemoteAddr();
                }

                for (int i = 0; i < domainList.size(); i++) {

                    //NEED TO WAIT 1 SECOND HERE / ANYWHERE IN LOOP



                    String singleDomain = domainList.get(i).toString();



                    SimpleSocketClient tester = new SimpleSocketClient(singleDomain,ipAddress);
                    pageHtml = tester.getResult();

                    try {

                        String whoIs2 = ipAddress + " " + ipAddress + " " + singleDomain + "\r\n";

                        byte[] data = whoIs2.getBytes();





                        //details of each domain

                        //domain name
                        domain = singleDomain;

                        //status

                        status = "FTR";


                        //registrant

                         registrant = "N/A";


                        //dates

                            dates = "N/A";


                        //tag

                            tag = "N/A";


                        //email

                            email = "N/A";

                        }



                    } catch (Exception e) {
                        Logger.getLogger("ip is " + ipAddress + bulkWhoIsCommand.class.getName()).log(Level.SEVERE, null, e);
                        forwardToJsp = "index.jsp";
                        return forwardToJsp;
                    }

                    //single one
                    theDomain = new Domain(domain,status,registrant,dates,tag,email);


                    //now add to arrayList
                    domains.add(theDomain);
//                try {
//                    Thread.sleep(230000);
//                } catch (InterruptedException ex) {
//                        Logger.getLogger(bulkWhoIsCommand.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                try {
//                    pause.poll(100 * 300, TimeUnit.MILLISECONDS);
//                } catch (InterruptedException ex) {
//                        Logger.getLogger(bulkWhoIsCommand.class.getName()).log(Level.SEVERE, null, ex);
//                }




                }

编辑 - 朋友建议使用ajax来轮询更新,但肯定有一种方法可以使用java。

1 个答案:

答案 0 :(得分:1)

您可以尝试在while循环中设置while循环,以暂停它。应该这样:

 while(!done)
 {
     long start = System.currentTimeMillis();
     while(System.currentTimeMillis() - start < 1000L){}
 }

没有测试它,但方法很重要。我有想法将两者结合起来。因此,每次Thread.Sleep()崩溃时,都必须进行循环。像这样:

while(!done)
{
     long start = System.currentTimeMillis();
     try {
         Thread.sleep(1000);
     } catch (InterruptedException ex) {
         System.err.println(e);
     }
     while(System.currentTimeMillis() - start < 1000L){}
}

当Thread.Sleep()工作时,它只被调用一次。否则你需要一些CPU时间。可能是cpu经济版。