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

Java 甚至可以让这个循环每次等待几秒钟吗?,java,jsp,sleep,Java,Jsp,Sleep,首先,是的,我是从网络浏览器中调用它的。这是一段相当长的代码,但我已经尽可能地缩短了它 基本上,我需要为循环中的每个迭代等待一秒钟。几乎什么都试过了(.sleep()等),但似乎没有暂停。我之所以需要这样做,是因为SimpleSocketClient正在调用一个每秒允许的下限的套接字 @Override public String execute(HttpServletRequest request, HttpServletResponse response) {

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

基本上,我需要为循环中的每个迭代等待一秒钟。几乎什么都试过了(.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);
//                }




                }
@覆盖
公共字符串执行(HttpServletRequest请求,HttpServletResponse响应){
字符串forwardToJsp=null;
HttpSession session=request.getSession();
字符串allUrls=request.getParameter(“域”);
ArrayList domainList=新的ArrayList();
扫描仪sc=新扫描仪(allUrls);
while(sc.hasNextLine()){
字符串行=sc.nextLine();
domainList.add(行);
//处理生产线
}
sc.close();
字符串pageHtml=null;
字符串域=”;
字符串状态=”;
字符串注册人=”;
字符串日期=”;
字符串标签=”;
字符串email=“”;
ArrayList域=新的ArrayList();
域;
字符串ipAddress=request.getHeader(“X-FORWARDED-FOR”);
如果(ipAddress==null){
ipAddress=request.getRemoteAddr();
}
对于(int i=0;i

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

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

 while(!done)
 {
     long start = System.currentTimeMillis();
     while(System.currentTimeMillis() - start < 1000L){}
 }
while(!done)
{
     long start = System.currentTimeMillis();
     try {
         Thread.sleep(1000);
     } catch (InterruptedException ex) {
         System.err.println(e);
     }
     while(System.currentTimeMillis() - start < 1000L){}
}
while(!done)
{
长启动=System.currentTimeMillis();
而(System.currentTimeMillis()-start<1000L){}
}
没有测试它,但方法很重要。我想把两者结合起来。所以每次Thread.Sleep()崩溃时,您都必须执行循环。大概是这样的:

 while(!done)
 {
     long start = System.currentTimeMillis();
     while(System.currentTimeMillis() - start < 1000L){}
 }
while(!done)
{
     long start = System.currentTimeMillis();
     try {
         Thread.sleep(1000);
     } catch (InterruptedException ex) {
         System.err.println(e);
     }
     while(System.currentTimeMillis() - start < 1000L){}
}
while(!done)
{
长启动=System.currentTimeMillis();
试一试{
睡眠(1000);
}捕获(中断异常例外){
系统错误println(e);
}
而(System.currentTimeMillis()-start<1000L){}
}

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

线程有什么问题。睡眠(1000);一秒钟?@Cruncher我猜:
Thread.sleep(1)
。@Gliptal错了,
Thread.sleep(230000)已尝试。首先阅读代码。@Andi
sleep
static
方法,您可以直接使用
Thread#sleep
,因为OP已经尝试了,但没有起作用。
Thread.sleep()
就是这样做的方法。如果它不起作用,OP应该找出原因。在servlet请求中休眠30秒或230秒是愚蠢的。可能您的容器中断线程的时间太长。这可能会起作用,但会使用大量CPU重复检查当前时间。这取决于您执行的频率。JIT将在运行时对此进行优化,代码执行速度将比最初执行时快。因此,在我的for(int i=0)中等等?我不知道你想做什么,但看起来它在正确的地方。