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()
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()
我开始的程序基本如下:
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吗?
^^^^----------so i think u need to create a thread, otherwise u can't use this method.
this.wait(5000);
}
但又出现了一个新问题,这样就使得我在框架网页中设置的<frameset onUnload="quit()">javascript句柄函数失效,在关闭框架时不能再执行已经做好的quit()函数。很是麻烦啊
不过你这种情况我没有遇到过,如果有什么好的解决方法能不能贴出来瞧瞧。^_^