use sleep or wait.

解决方案 »

  1.   

    1.java.lang 
    Class Thread
    java.lang.Object
      |
      +-java.lang.Threadsleep
    public static void sleep(long millis)
                      throws InterruptedException
    Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors. Parameters:
    millis - the length of time to sleep in milliseconds. 
    Throws: 
    InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
    See Also:
    Object.notify()
    2.
    java.lang.Objectwait
    public final void wait(long timeout)
                    throws InterruptedException
    Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. 
    The current thread must own this object's monitor. This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens: Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened. 
    Some other thread invokes the notifyAll method for this object. 
    Some other thread interrupts thread T. 
    The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified. 
    The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked. 
    If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above. Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits. This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor. 
    Parameters:
    timeout - the maximum time to wait in milliseconds. 
    Throws: 
    IllegalArgumentException - if the value of timeout is negative. 
    IllegalMonitorStateException - if the current thread is not the owner of the object's monitor. 
    InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
    See Also:
    notify(), notifyAll()
      

  2.   

    我知道你说的,但是,我用了wait()和sleep(),页面却报错说是current Thread not owner
    我开始的程序基本如下:
    for(int i=0;i<100;i++)
     {
      out.print(message);//message就是要输出的信息
      for(int p=0;p<100000000;p++){}
      out.flush();
     }
    这个程序是能够运行的,而且有层次感,但,cpu占用率太高;
    我用了wait()方法就是把for(int p=0;p<100000000;p++){}替换成了wait(1000),但此时就报错了,请问各位我该如何修改以上程序来利用wait,和sleep方法,难道要建立一个继承了线程类的bean吗?
      

  3.   

    This method should only be called by a thread................
                       ^^^^----------so i think u need to create a thread, otherwise u can't use this method.
      

  4.   

    我也认为必须建立一个thread的bean来实现,并且也的确实现了,不过我总觉得自己的方法不是很规范,我想请有过这方面经验的高手能给一点代码例程参考参考.
      

  5.   

    synchronized(this){
        this.wait(5000);
    }
      

  6.   

    这个问题我已经解决了,具体方法如下,现在jsp中导入Thread类,然后用Thread.currentTread().sleep(1000);就可以让当前网页的输出暂停了。
    但又出现了一个新问题,这样就使得我在框架网页中设置的<frameset onUnload="quit()">javascript句柄函数失效,在关闭框架时不能再执行已经做好的quit()函数。很是麻烦啊
      

  7.   

    什么意思?你是说你的sleep放在框架网页里面?是不是时间还没到,就希望关闭框架时执行quit()函数?如果是,那么我想你要在关闭之前判断一下,然后wakeup该线程。(个人想法,不知可不可行)
    不过你这种情况我没有遇到过,如果有什么好的解决方法能不能贴出来瞧瞧。^_^
      

  8.   

    其实我是在做一个聊天室,通过sleep来循环输出聊天信息而不用每次刷新信息页面。聊天室的总页面是一个框架页面这个大家都应该清楚,在关闭聊天室时必须要同时清除当前用户的session和application信息,于是只有通过javascript的onUnload句柄捕捉窗口关闭的事件,由于框架中的每个页面都有可能在聊天过程中被刷新,所以,该onUnload句柄只能放在框架的总页面上。但由于框架中的聊天信息输出页面一直属于输出状态,就造成了整个框架页面都处于载入状态而非已经载入状态,这就导致了onUnload句柄无法被捕捉,不知各位明白了我的意思没有,有这方面经验的人能给点指教吗?