java void方法
对象类最终void wait() (Object Class final void wait())
This method is available in java.lang.Object.wait().
此方法在java.lang.Object.wait()中可用。
This method causes the current thread to wait until another thread notification by calling notify() or notifyAll() method of the object.
通过调用对象的notify()或notifyAll()方法,此方法使当前线程等待直到另一个线程通知。
This method throws an InterruptedException when other thread interrupted current thread.
当其他线程中断当前线程时,此方法将引发InterruptedException。
This method can't override because it is final.
此方法是最终方法,因此无法覆盖。
Syntax:
句法:
final void wait(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the Object class.
我们没有在Object类的方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is void that means this method returns nothing after execution.
该方法的返回类型为void ,这意味着该方法在执行后不返回任何内容。
Java程序演示对象类的wait()方法的示例 (Java program to demonstrate example of Object Class wait() method)
import java.lang.Object;
public class Thread1 {
public static void main(String[] args) throws InterruptedException {
// Create an object of Thread2
Thread2 t2 = new Thread2();
// By calling start() so Thread start() will exceute
t2.start();
synchronized(t2) {
System.out.println("Main thread trying to call wait()");
// By calling wait() causes the current thread to
// wait until execution of another thread
t2.wait();
System.out.println("Main Thread get notification here");
System.out.println(t2.total);
}
}
}
class Thread2 extends Thread {
int total = 0;
public void run() {
synchronized(this) {
System.out.println("Thread t2 starts notification");
for (int i = 0; i < 100; ++i) {
total = total + i;
}
System.out.println("Thread t2 trying to given notification");
this.notify();
}
}
}
Output
输出量
D:\Programs>javac Thread1.java
D:\Programs>java Thread1
Main thread trying to call wait()
Thread t2 starts notification
Thread t2 trying to given notification
Main Thread get notification here
4950
翻译自: https://www.includehelp.com/java/object-class-final-void-wait-method-with-example.aspx
java void方法